Newbie mono question

GeneAllenGeneAllen USMember
edited January 2013 in Xamarin.iOS

I'm an experienced .net programmer and would like to do something seems like it should be simple to me.

I have a simple list (UITableView) and I want to add a single button below it. When you click the button, I want to show simple dialog that will have a text field and a 'Connect' button.

I'm missing something. How do I launch a dialog when a button is pressed?

here's my button action:

    UIWindow _window;
    partial void bnSettings (MonoTouch.Foundation.NSObject sender)
    {
        RootElement root = new RootElement("Login");
        Section section = new Section("Credential");
        root.Add(section);
        section.Add(new EntryElement("Login", "Enter your login", ""));
        section.Add(new EntryElement("Password", "", "", true));

        DialogViewController dlg = new DialogViewController(root, true);
        _window = new UIWindow(UIScreen.MainScreen.Bounds);
        _window.RootViewController = dlg;
        _window.MakeKeyAndVisible();
    }

the dialog does indeed show and I can enter stuff, but I have two problems.
1. there isn't a back button...(I'm guessing that's because the view stack is empty)
2. how do I show a button in the dialog?

I'm sure that there is something simple I'm missing...can someone point it out?

thanks

Gene

Posts

  • GeneAllenGeneAllen USMember

    Sorry, I thought I had formatted the code correctly. I have no idea why only 1/2 of the code looks like it should.

  • NicWiseNicWise NZMember, Insider, Beta mod

    Hi @geneallen

    1. Correct. Unless you push something else, there is no back button.
    2. Have a look at the dlg's NavigationItem - there is a RightBarButtonItem(s) and LeftBarButtonItem(s) which you can assign a UIBarButton (or an array of them) to, and trigger things off.

    If you make a left one, the back button will override it I think.

    I tend to inherit off DialogViewController, and set this kind of thing up in the constructor.

  • GeneAllenGeneAllen USMember

    Thanks nicwise for the guidance.

    Per your suggestion, I added this line:

    dlg.NavigationItem.SetLeftBarButtonItem (new UIBarButtonItem (UIBarButtonSystemItem.Done, delegate {
                int a = 0;
            }), true);
    

    Still no joy.

    Am I even taking the right approach here?

  • NicWiseNicWise NZMember, Insider, Beta mod

    I think the bit you are missing is:

    //false because you are not pushing it this time
    DialogViewController dlg = new DialogViewController(root, false);
    var navc = new UINavigationController(dlg);
    _window = new UIWindow(UIScreen.MainScreen.Bounds);
    _window.RootViewController = navc;
    

    That should get you the bar on the top, which is where the buttons go.

  • GeneAllenGeneAllen USMember

    Woohoo! the button is showing up! I think the key bit was the UINavigationController.

    so now...the question is...how do I dismiss the dialog?

    I've tried the dlg.DeactivateController(true) and navc.DismissViewController(true, null)

    Neither one worked.

    Is there a link somewhere that explains how to do all things dialog? I hate asking such silly questions.

    thank you for all your help NicWise.

  • CodeMonkeyCodeMonkey GBMember ✭✭✭

    Have you found the samples?

    http://docs2.xamarin.com/iOS/Samples/AllSamples

    I find I can answer most of my questions by referring to these.

  • NicWiseNicWise NZMember, Insider, Beta mod

    @geneallen how do you mean dismiss it?

    That code in your btnSettings is making a new root window, so it's the "top", there is nothing to dismiss :)

        RootElement root = new RootElement("Login");
        Section section = new Section("Credential");
        root.Add(section);
        section.Add(new EntryElement("Login", "Enter your login", ""));
        section.Add(new EntryElement("Password", "", "", true));
    
        DialogViewController dlg = new DialogViewController(root, true);
        _window = new UIWindow(UIScreen.MainScreen.Bounds);
        _window.RootViewController = dlg;
        _window.MakeKeyAndVisible();
    

    If you want to show this as a popup, you need to use PresentModalViewController:

        RootElement root = new RootElement("Login");
        Section section = new Section("Credential");
        root.Add(section);
        section.Add(new EntryElement("Login", "Enter your login", ""));
        section.Add(new EntryElement("Password", "", "", true));
    
        DialogViewController dlg = new DialogViewController(root, true);
        //at this point, you can present it if you want a modal dialog:
        //you could do this in 2 lines, and keep a ref to the UINavViewCtrl if you want.
        PresentModalViewController(new UINavigationViewController(dlg), true);
    
        //OR (but not both) just push it
        NavigationController.PushViewController(dlg, true);
    

    the true on the end, in both cases, is to make it animate. The true on the end of the DialogViewController constructor is if it is being pushed, to technically, if you do the modal "Present", that should be false.

    you can then get rid of a Modal one with

    ParentViewController.DismissModalViewControllerAnimated(true);
    

    In this case, ParentViewController is the one you did the Present from....

    Does that make sense?

    This might help too:

    https://github.com/nicwise/londonbikeapp/blob/master/londonbikeapp/TripLogViewController.cs#L96

    The code in that project is REALLY REALLY old, and it might not even build anymore, but it also might help you :)

  • GeneAllenGeneAllen USMember

    Thank you for all your help.

    The mistake I made was starting with Hello World instead of TaskyTouch.

Sign In or Register to comment.