Is there a proper way to use Monotouch.Dialog (iOs) and call a UIViewController when a user clicks on a RootElement? I'm building a page of data based on an array and when clicked I'd like to open this custom view and pass in the array element.. Something like this (doesn't work). Any help is appreciated.
RootElement CreateMenuCategory(JToken menucat) {
RootElement MenuCategory = new RootElement(menucat["menucategoryname"].Value<String>());
RootElement root_element;
Section section = new Section();
foreach(JToken menuitem in menucat["menuitems"]) {
root_element = new RootElement(menuitem["menuitemname"].Value<String>(), (RootElement e) => {
return _menuitemView.LoadMenuItem(menuitem); // menuitem on view is always the same
});
section.Add (root_element);
}
MenuCategory.Add (section);
return MenuCategory;
}
That code doesn't work as the delegate always passes in the same element every time.
This is merely a side effect of how the "menuitem" variable is captured by the lambda function.
Change your foreach loop to look like this instead:
foreach (JToken iteratorMenuitem in menucat ["menuitems"]){
var menuitem = iteratorMenuitem;
//.. the rest
Related
I am creating an iPad app using MonoTouch 2.10.11 and I want to MonoTouch.Dialog to create some of the editable fields on a form. One of the fields will use a RadioGroup to allow the user to select from a list of options. The default behavior of M.T.D is display the selection list table over the existing table. This works great for the iPhone layout, but on this iPad form, the table is only on a small area of the form and the navigation bar looks odd on the middle of the form. I want to display the selection as a full screen modal and the user will hit a "back" button to go back the previous form with the selected item.
I created a new RootElement descendant class like this:
public class ModalRootElement : RootElement
{
public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath path)
{
tableView.DeselectRow (path, false);
UIViewController uIViewController = this.MakeViewController ();
this.PrepareDialogViewController (uIViewController);
dvc.PresentViewController (uIViewController, true, null);
}
protected override void PrepareDialogViewController(UIViewController dvc)
{
base.PrepareDialogViewController(dvc);
UIButton button = UIButton.FromType (UIButtonType.RoundedRect);
button.Frame = new RectangleF (5, 5, 80, 20);
button.SetTitle ("back", UIControlState.Normal);
button.TouchUpInside += delegate {
DialogViewController d = dvc as DialogViewController;
(d.Root as ModalRootElement).TableView.ReloadData ();
d.DeactivateController(true);
};
dvc.View.AddSubview (button);
}
}
The table is implemented with the following code:
var _status = new ModalRootElement("Status", new RadioGroup("status", -1)) {
(new Section() {
new RadioElement("New", "status"),
new RadioElement("In process", "status"),
new RadioElement("Rejected", "status"),
new RadioElement("Deferred", "status"),
new RadioElement("Transferred", "status"),
new RadioElement("Unknown", "status"),
new RadioElement("Complete", "status")
})
};
var _odom = new EntryElement ("Odometer", "current odom", "");
_odom.KeyboardType = UIKeyboardType.DecimalPad;
_odom.TextAlignment = UITextAlignment.Right;
var root = new RootElement ("back") {
new Section("") {
_status,
_odom
}
};
_dvc = new DialogViewController(root);
_nav = new UINavigationController (_dvc);
_nav.SetNavigationBarHidden (true, false);
When I run the app, I can drill into the RadioGroup and make a selection. When I click the back button that I added to the view, the modal view closes and the RadioSelected properted of the ModalRootElement object is set correctly, but the text is not displayed.
If I change Selected() method to call dvc.ActivateController instead of PresentViewController, the ModalRootElement displays the correct text, but the RadioGroup table has the wrong size. Is there a way to get the RootElement to display the correct text when you use PresentViewController instead of ActivateController?
I think you need a Root.Reload() call.
When an item is clicked, a details view controller is opened that makes some webservice calls to save back the data if the user clicks save (this works fine). The issue I have is after saving the data when the user clicks back to go back to the list, the list item (ie if the name was updated) is not updated. So, perhaps the user changes the name and so the list prior should be updated, I'd like to update the value without having to make another webservice call to repopulate.
I am calling this.ReloadData() when the view appears, but this never seems to update the list names. I've checked workouts which is a list of objects, and it has been modified by the detail controller, however the reload data doesn't seem to do anything.
How do I refresh the data in the case that another controller has modified the list of objects.
public override void ViewWillAppear (bool animated)
{
InvokeOnMainThread (delegate {
this.ReloadData();
}); // call reload on main thread
base.ViewWillAppear (animated);
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
workouts = manager.GetWorkouts(Helpers.User.Auth);
var sections = new Section();
var root = new RootElement("Workouts"){sections};
foreach (var wo in workouts) {
var tempwo = wo;
var wodetail = new RootElement(wo.Name, delegate {
var W = new WorkoutViewModel(tempwo);
var p = new WorkoutDetailController(W, tempwo);
return p;
});
sections.Add (wodetail);
}
Root = root;
}
It seems that when you assign the value of an element from a custom object like so:
AccountDetailsSection = new Section ("Details"){
new StringElement ("Code:", _Account.Code),
new StringElement ("Name:", _Account.Name) };
You get the desired result but the element loses the pointer back to the property of the object. So if you change the value of the property later for example:
_Account.Code = "xxx";
Reloading the data doesn't do anything. You should manually update the value like so:
(AccountDetailsSection.Elements [0] as StringElement).Value = "xxx";
this.Root.Reload ( AccountDetailsSection, UITableViewRowAnimation.Fade);
Hope this Helps.
I have a RootElement with Sections. One section has RadioElements. How do I change the BackgroundView on the sub section?
Something like this doesn't work because the sub view isn't yet created.:
rootGroups = new RootElement ("Ideas", rdoGroup) {
new RootElement ("Baha'i") {
new Section (){
new RadioElement ("Peace"),
new RadioElement ("Unity"),
new RadioElement ("Science")
}
}
};
???
rootGroups.TableView.BackgroundColor = ...;
If you want to create two different view with two different background you must create another dialogViewController for the second root, that way you will be able to change the background.
To do this, instead of using a regular RootElement constructor, use the RootElement constructor that takes a delegate method that can create the UIViewController on demand.
Hi i m added the File menu item creating the views, my code look like
final Class viewClass : new Class[] { Dashboard.class,
Editor.class, Ticket.class, MockupView.class }) {
navigator.addView(viewClass.getSimpleName(), viewClass);
menu.addItem(viewClass.getSimpleName(), new MenuBar.Command() {
public void menuSelected(MenuItem selectedItem) {
navigator.navigateTo(viewClass);
}
});
but how to add sub menu item inside this menu, any one can help me if you provide me example it will be great full for me
menu.addItem returns a reference to the new menu item, which you can then use to add child items:
MenuBar.MenuItem item = menu.addItem("Parent", null);
item.addItem("Child", null);
Everything you need to know is on this Vaadin documentation page
Are there any existing extensions or is it fairly straight forward to add styles to RootElement in monotouch.dialog in a similar way you can style StyledStringElement.
Basically I would like to add an image, or badge to RootElement to indicate what sort of details would be in the child view, eg add Success, Warning, Error, Info type image - so the users may only be interested in clicking through to details that are not fully successful.
So ideally I would be able to code something like this...
UIImage imageSuccess = ImageLoader.DefaultRequestImage (new Uri ("file://" + Path.GetFullPath ("Images/Success.png")), null);
var root = new RootElement("Root") {
Image = imageSuccess,
Accessory = UITableViewCellAccessory.DetailDisclosureButton,
new Section (){
new BooleanElement ("Airplane Mode", false),
new RootElement ("Notifications") {
new Section (null, "Turn off Notifications")
{
new BooleanElement ("Notifications", false)
}
}}
};
Thanks for any help or pointers.
This question is old, but if anyone else comes across it you can subclass the RootElement class to add an icon. My code is as follows:
public class ImageRootElement : RootElement
{
private UIImage _image;
public override MonoTouch.UIKit.UITableViewCell GetCell (MonoTouch.UIKit.UITableView tv)
{
var baseCell = base.GetCell (tv);
var cell = new UITableViewCell (UITableViewCellStyle.Subtitle, "cellId");
cell.TextLabel.Text = Caption;
cell.Accessory = baseCell.Accessory;
cell.ImageView.Image = _image;
return cell;
}
public ImageRootElement (string caption, UIImage image) : base(caption)
{
_image = image;
}
}
Since MT.Dialog is open source, you can modify the RootElement properties and constructors however you like. I don't think there's anything that does what you want right out of the box, so you'll have to extend Dialog to meet your needs.
As an aside, it sounds like you MAY be misunderstanding the intent of RootElement. RootElement is simply the main container that all of your sections and elements are in. It doesn't seem to make sense to have a disclosure indicator or badge on a RootElement, simply because that is not the intent of RootElement. It's possible that I could just be misunderstanding you. If, however, you want to do custom styling with badges, etc, on an element, you can create custom element classes that inherit from OwnerDrawnElement, overriding it's two abstract methods. However, read Miguel's answer to a similar question here before doing so.