How do I make DateTimeElement datePicker show up in a popover?

classic Classic list List threaded Threaded
6 messages Options
Xy
Reply | Threaded
Open this post in threaded view
|

How do I make DateTimeElement datePicker show up in a popover?

Xy
This post was updated on .
The default date picker looks like this:


I'd like it to appear in a popover instead, so I created my own class:
        public class CustomDateTimeElement : DateTimeElement
        {
                static bool UserInterfaceIdiomIsPhone {
                        get { return UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone; }
                }
               
                UIPopoverController PopoverVC;

                public CustomDateTimeElement (string caption, DateTime date)
                        : base(caption, date)
                {
                }

                public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath path)
                {
                        if (UserInterfaceIdiomIsPhone) {
                                base.Selected(dvc, tableView, path);
                        }
                        else {
                                // Trying to make keyboard go away. doesn't work
                                tableView.CellAt(path).BecomeFirstResponder();

                                if (datePicker == null) {
                                        datePicker = CreatePicker();
                                }

                                // Put picker in a view controller
                                var navVC = new UIViewController();
                                navVC.View.BackgroundColor = UIColor.Black;
                                navVC.View.AddSubview(datePicker);

                                // Create a popover that fits picker and show it
                                PopoverVC = new UIPopoverController(navVC) {
                                        PopoverContentSize = new System.Drawing.SizeF(320, 215)
                                };
                                PopoverVC.PresentFromRect(tableView.RectForRowAtIndexPath(path), tableView, UIPopoverArrowDirection.Any, true);
                        }
                }
        }

The picker appears fine, but when I change the date value, the corresponding DateTimeElement value does NOT change. Also, as noted above, I was unsuccessful in making any text field resign first responder. Could anyone give me some guidance on how to fix these two issues?
Xy
Reply | Threaded
Open this post in threaded view
|

Re: How do I make DateTimeElement datePicker show up in a popover?

Xy
Updated original post to correct code formatting
Reply | Threaded
Open this post in threaded view
|

Re: How do I make DateTimeElement datePicker show up in a popover?

Jeff Stedfast
Hi Xy,

I wrote my own Element subclass for this in my own side-project. You can find the source for my DateEntryElement here:


Hope that helps!

Jeff

On Thu, Feb 21, 2013 at 2:34 AM, Xy <[hidden email]> wrote:
Updated original post to correct code formatting



--
View this message in context: http://monotouch.2284126.n4.nabble.com/How-do-I-make-DateTimeElement-datePicker-show-up-in-a-popover-tp4658091p4658092.html
Sent from the MonoTouch mailing list archive at Nabble.com.
_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch


_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch
Xy
Reply | Threaded
Open this post in threaded view
|

Re: How do I make DateTimeElement datePicker show up in a popover?

Xy
Hi Jeff,

Thanks to your code, I added these two methods to make value update correctly:
                public override UIDatePicker CreatePicker ()
                {
                        var picker = base.CreatePicker ();

                        if (!UserInterfaceIdiomIsPhone) {
                                picker.ValueChanged += HandleValueChanged;
                        }
                        return picker;
                }

                void HandleValueChanged (object sender, EventArgs e)
                {
                        this.DateValue = (DateTime) ((UIDatePicker)sender).Date;
                        GetImmediateRootElement().Reload(this, UITableViewRowAnimation.None);
                }

Do you have any suggestion for dismissing the keyboard when the picker is up?
Xy
Reply | Threaded
Open this post in threaded view
|

Re: How do I make DateTimeElement datePicker show up in a popover?

Xy
Never mind. I figured out a way to show the picker on dismissing the keyboard. Basically added these two observers in the constructor (and removed them in Dispose()):
                        KeyboardShowObserver = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.DidShowNotification, delegate {
                                IsKeyboardVisible = true;
                        });

                        KeyboardHideObserver = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.DidHideNotification, delegate {
                                IsKeyboardVisible = false;

                                if (ShouldShowPicker) {
                                        ShowPicker(WeakTableView, WeakPath);

                                        // Don't keep these around
                                        ShouldShowPicker = false;
                                        WeakTableView = null;
                                        WeakPath = null;
                                }
                        });

I set the variables ShouldShowPicker, WeakTableView, and WeakPath in Selected() if keyboard is visible. Let me know if there's a better way.
Reply | Threaded
Open this post in threaded view
|

Re: How do I make DateTimeElement datePicker show up in a popover?

sagheer
This post was updated on .
In reply to this post by Xy
HI,
using your code, how can i fire even DateSelected? from the point where i created element.

e.g.
<nabble_embed>var element = new DateTimePickerElement(caption, (value != null ? (DateTime)value : DateTime.Now)); return element;</nabble_embed>