Jamey McElveen
Jamey McElveen

Reputation: 18325

In MonoTouch.Dialog what do I need to do to make datetime pickers have and "ok" and "cancel" option?

In MonoTouch.Dialog what do I need to do to make datetime pickers have and "ok" and "cancel" option?

In this example code when you click a DateTime element you navigate to the picker screen there is no way to select or cancel after you navigate to the date picker.

[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
    protected UIWindow window;
    protected UINavigationController navigationController;
    protected RootElement rootElement;
    protected DialogViewController dialogViewController;        

    public override bool FinishedLaunching (UIApplication app, NSDictionary options)
    {
        window = new UIWindow (UIScreen.MainScreen.Bounds);
        var navigationController = new UINavigationController();
        window.AddSubview (navigationController.View);
        window.MakeKeyAndVisible ();

        rootElement = CreateJsonItems();
        dialogViewController = new DialogViewController (rootElement, true);
        navigationController.PushViewController (dialogViewController, true);

        return true;
    }

    protected RootElement CreateJsonItems()
    {
        var json = 
            @"{
                ""title"": ""Json Sample"",
                ""sections"": [{
                    ""header"": ""Dates and Times"",
                    ""elements"": [{
                        ""type"": ""datetime"",
                        ""caption"": ""Date and Time"",
                        ""value"": ""Sat, 01 Nov 2008 19:35:00 GMT""
                    }, {
                        ""type"": ""date"",
                        ""caption"": ""Date"",
                        ""value"": ""10/10""
                    }, {
                        ""type"": ""time"",
                        ""caption"": ""Time"",
                        ""value"": ""11:23""
                    }]
                }]
            }";

        using(var reader = new StringReader(json))
        {
            var jsonObject = JsonObject.Load(reader) as JsonObject;
            var jsonElement = JsonElement.FromJson(jsonObject);
            return jsonElement;
        }
    }
}

Thank you Poupou! after applying your advice this is the code that fixed it.

public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
    window = new UIWindow (UIScreen.MainScreen.Bounds);
    rootElement = CreateJsonItems();
    var dialogViewController = new DialogViewController(rootElement);
    navigationController = new UINavigationController(dialogViewController);
    window.Add (navigationController.View);
    window.MakeKeyAndVisible ();
    return true;
}

Upvotes: 3

Views: 564

Answers (1)

poupou
poupou

Reputation: 43553

When used like this in MonoTouch.Dialog the pickers are not modal. So it's the navigation UI that will let you go back to the previous view. OTOH that will be hidden by doing this:

   var navigationController = new UINavigationController();
   window.AddSubview (navigationController.View);

note: it's also a (different) problem since that won't keep a reference to navigationController.

You should try to replace the above (and move it after the creation of your DialogViewController) with:

   window.RootViewController = dialogViewController;

That should let the default navigation UI come back and allow you to go back from the date picker.

Upvotes: 3

Related Questions