Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7807357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:49:59+00:00 2026-06-02T02:49:59+00:00

I’ve searched the interwebs to no avail… I am using MT.D and want to

  • 0

I’ve searched the interwebs to no avail… I am using MT.D and want to set a birthdate for a person using the DateElement, but the birthdate could be null, meaning that the data has not been collected yet. Anyone know how to make a DateElement accept a null value OR a date?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:50:01+00:00Added an answer on June 2, 2026 at 2:50 am

    UPDATE 20140106: Since iOS7 has been released, Apple desires that date/time pickers be inline with content rather than be an action sheet, or in this case, full screen overlay. Therefore, this code is for how-to and historical purposes only.

    Ok, so I rolled my own class. Personally I think the current date/time picker set up doesn’t look as professional as having the equivalent of an ActionSheet pop up with a datepicker attached to it. Someone more experienced in MT.D may be able to figure it out, but what I did was to copy the code from DateTimeElement and DateElement and modify it so that it has three buttons on top: The left-most button is Cancel, the right button area has “Set” and “Null” buttons. The captions for the right buttons can be set to whatever you like in the ctor of the class, but can be defaulted to “Set Date” and “No Date”.

    Sharing is caring!

    NULLABLE DATE TIME ELEMENT

    using MonoTouch.Foundation;
    using MonoTouch.UIKit;
    using System;
    using System.Drawing;
    
    namespace MonoTouch.Dialog
    {
        public class NullableDateTimeElement : StringElement
        {
            private class MyViewController : UIViewController
            {
                private NullableDateTimeElement container;
                private bool hasNullValue = false;
                private bool hasBeenSet = false;
                //private EventHandler nullButtonTouched;
                //UIButton isNullButton;
                public bool Autorotate
                {
                    get;
                    set;
                }
                public MyViewController (NullableDateTimeElement container)
                {
                    this.container = container;
                }
                public override void ViewDidLoad ()
                {
                    base.ViewDidLoad ();
                    //isNullButton = UIButton.FromType (UIButtonType.RoundedRect);
                    //isNullButton.SizeToFit ();
                    //isNullButton.Frame = new RectangleF(this.View.Frame.Top, this.View.Frame.Left, this.View.Frame.Width - 40f, 40f);
                    //isNullButton.SetTitle (container.NullButtonCaption, UIControlState.Normal);
                    this.NavigationItem.RightBarButtonItems = new UIBarButtonItem[]
                    {
                        new UIBarButtonItem(container.NullButtonCaption, UIBarButtonItemStyle.Done, NullButtonTapped),
                        new UIBarButtonItem(container.SetButtonCaption, UIBarButtonItemStyle.Done, SetButtonTapped)
                    };
                    this.NavigationItem.LeftBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Cancel, CancelTapped);
                    this.NavigationItem.HidesBackButton = true;
                    //this.View.AddSubview (isNullButton);
                    //this.isNullButton.TouchUpInside += (nullButtonTouched = new EventHandler(nullButtonWasTouched)); 
                }
    
                void CancelTapped(object sender, EventArgs e)
                {
                    hasBeenSet = false;
                    this.NavigationController.PopViewControllerAnimated (true);
                }
    
                void NullButtonTapped(object sender, EventArgs e)
                {
                    hasBeenSet = true;
                    hasNullValue = true;
                    this.NavigationController.PopViewControllerAnimated (true);
                }
    
                void SetButtonTapped(object sender, EventArgs e)
                {
                    hasBeenSet = true;
                    hasNullValue = false;
                    this.NavigationController.PopViewControllerAnimated (true);
                }
    
                public override void ViewWillDisappear (bool animated)
                {
                    base.ViewWillDisappear (animated);
                    if (hasBeenSet)
                    {
                        if (!hasNullValue)
                            this.container.DateValue = this.container.datePicker.Date;
                        else
                            this.container.DateValue = null;
                    }
                    //this.isNullButton.TouchUpInside -= nullButtonTouched;
                    //nullButtonTouched = null;
                }
                /*void nullButtonWasTouched(object sender, EventArgs e)
                {
                    hasNullValue = true;
                    NavigationController.PopViewControllerAnimated (true);
                }*/
                public override void DidRotate (UIInterfaceOrientation fromInterfaceOrientation)
                {
                    base.DidRotate (fromInterfaceOrientation);
                    this.container.datePicker.Frame = NullableDateTimeElement.PickerFrameWithSize (this.container.datePicker.SizeThatFits (SizeF.Empty));
                }
                public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
                {
                    return this.Autorotate;
                }
            }
            public DateTime? DateValue;
            public UIDatePicker datePicker;
            //public UIButton isNullButton;
            public string NullButtonCaption { get; set; }
            public string SetButtonCaption { get; set; }
    
            protected internal NSDateFormatter fmt = new NSDateFormatter
            {
                DateStyle = NSDateFormatterStyle.Short
            };
    
            public NullableDateTimeElement (string caption, DateTime? date, string nullButtonCaption, string setButtonCaption) : base (caption)
            {
                this.DateValue = date;
                this.Value = this.FormatDate (date);
                this.NullButtonCaption = nullButtonCaption;
                this.SetButtonCaption = setButtonCaption;
            }
    
            public NullableDateTimeElement(string caption, DateTime? date, string nullButtonCaption) : this(caption, date, nullButtonCaption, "Set Date")
            {}
    
            public NullableDateTimeElement(string caption, DateTime? date) : this(caption, date, "No Date", "Set Date")
            {}
    
            public override UITableViewCell GetCell (UITableView tv)
            {
                this.Value = this.FormatDate (this.DateValue);
                UITableViewCell cell = base.GetCell (tv);
                cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
                return cell;
            }
            protected override void Dispose (bool disposing)
            {
                base.Dispose (disposing);
                if (disposing)
                {
                    if (this.fmt != null)
                    {
                        this.fmt.Dispose ();
                        this.fmt = null;
                    }
    /*              if (this.isNullButton != null)
                    {
                        this.isNullButton.Dispose ();
                        this.isNullButton = null;
                    }*/
                    if (this.datePicker != null)
                    {
                        this.datePicker.Dispose ();
                        this.datePicker = null;
                    }
                }
            }
            public virtual string FormatDate (DateTime? dt)
            {
                if (dt.HasValue)
                    return this.fmt.ToString (dt.Value) + " " + dt.Value.ToLocalTime ().ToShortTimeString ();
                else
                    return NullButtonCaption;
            }
            public virtual UIDatePicker CreatePicker ()
            {
                return new UIDatePicker (RectangleF.Empty)
                {
                    AutoresizingMask = UIViewAutoresizing.FlexibleWidth,
                    Mode = UIDatePickerMode.DateAndTime,
                    Date = this.DateValue ?? DateTime.Now
                };
            }
            private static RectangleF PickerFrameWithSize (SizeF size)
            {
                RectangleF applicationFrame = UIScreen.MainScreen.ApplicationFrame;
                float y = 0f;
                float x = 0f;
                switch (UIApplication.SharedApplication.StatusBarOrientation)
                {
                    case UIInterfaceOrientation.Portrait:
                    case UIInterfaceOrientation.PortraitUpsideDown:
    
                        {
                            x = (applicationFrame.Width - size.Width) / 2f;
                            y = (applicationFrame.Height - size.Height) / 2f - 25f;
                            break;
                        }
                    case UIInterfaceOrientation.LandscapeRight:
                    case UIInterfaceOrientation.LandscapeLeft:
    
                        {
                            x = (applicationFrame.Height - size.Width) / 2f;
                            y = (applicationFrame.Width - size.Height) / 2f - 17f;
                            break;
                        }
                }
                return new RectangleF (x, y, size.Width, size.Height);
            }
            public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath path)
            {
                NullableDateTimeElement.MyViewController myViewController = new NullableDateTimeElement.MyViewController (this)
                {
                    Autorotate = dvc.Autorotate
                };
                this.datePicker = this.CreatePicker ();
                this.datePicker.Frame = NullableDateTimeElement.PickerFrameWithSize (this.datePicker.SizeThatFits (SizeF.Empty));
                myViewController.View.BackgroundColor = UIColor.Black;
                myViewController.View.AddSubview (this.datePicker);
                dvc.ActivateController (myViewController);
            }
        }
    }
    

    NULLABLE DATE-ONLY ELEMENT

    using System;
    using MonoTouch.Foundation;
    using MonoTouch.UIKit;
    
    
    namespace MonoTouch.Dialog
    {
        public class NullableDateElement : NullableDateTimeElement
        {
            public NullableDateElement (string caption, DateTime? date, string nullButtonCaption, string setButtonCaption) : base (caption, date, nullButtonCaption, setButtonCaption)
            {
                initDateOnlyPicker ();
            }
    
            public NullableDateElement (string caption, DateTime? date, string nullButtonCaption) : base(caption, date, nullButtonCaption)
            {
                initDateOnlyPicker ();
            }
    
            public NullableDateElement (string caption, DateTime? date) : base(caption, date)
            {
                initDateOnlyPicker ();
            }
    
    
            void initDateOnlyPicker()
            {
                this.fmt.DateStyle = NSDateFormatterStyle.Medium;
            }
    
            public override string FormatDate (DateTime? dt)
            {
                if (dt.HasValue)
                    return this.fmt.ToString (dt);
                else
                    return base.NullButtonCaption;
            }
            public override UIDatePicker CreatePicker ()
            {
                UIDatePicker uIDatePicker = base.CreatePicker ();
                uIDatePicker.Mode = UIDatePickerMode.Date;
                return uIDatePicker;
            }
        }
    }
    

    @Miguel, Please consider adding this to MonoTouch.Dialog, as there is a very legitimate business need for null dates/date times, and this solution seems to do the trick. My code will have to be cleaned up a bit, but this works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am using JSon response to parse title,date content and thumbnail images and place
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.