2

インターウェブを検索しても役に立ちませんでした... MT.D を使用していてDateElement、 を使用して人の生年月日を設定したいのですが、生年月日が null である可能性があります。これは、データがまだ収集されていないことを意味します。DateElement が null 値または日付を受け入れるようにする方法を知っている人はいますか?

4

1 に答える 1

2

更新20140106: iOS7 がリリースされて以来、Apple は、日付/時刻ピッカーがアクション シートではなく、コンテンツとインラインであること、またはこの場合は全画面オーバーレイであることを望んでいます。したがって、このコードはハウツーおよび歴史的な目的のみを目的としています。

わかりましたので、私は自分のクラスをロールバックしました。ActionSheet個人的には、現在の日付/時刻ピッカーの設定は、日付ピッカーが添付されたポップアップに相当するものほどプロフェッショナルに見えないと思います。MT.D の経験が豊富な人なら理解できるかもしれませんが、私がしたことは、コードをコピーしてDateTimeElementDateElement上部に 3 つのボタンが表示されるように変更することでした。一番左のボタンはキャンセル、右のボタン領域は「設定」ボタンと「Null」ボタンがあります。右側のボタンのキャプションは、クラスの ctor で好きなように設定できますが、デフォルトで「日付を設定」および「日付なし」に設定できます。

共有は思いやりです!

NULL可能日時要素

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);
        }
    }
}

NULL可能日付のみの要素

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、これをMonoTouch.Dialogに追加することを検討してください。nullの日付/日付時刻に対する非常に正当なビジネスニーズがあり、このソリューションはうまくいくようです。私のコードは少しクリーンアップする必要がありますが、これでうまくいきます。

于 2012-04-18T15:16:29.297 に答える