public
Created

Update to ActionSheetDatePicker to set initial date to value in a text field

  • Download Gist
gistfile1.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
// Original code from: http://www.apress.com/9781430231745.
// Updated to deal with limitations explained here:
// http://www.distribucon.com/blog/ActionSheetDatePickerEnhancements.aspx
 
using System;
using MonoTouch.UIKit;
using System.Drawing;
using MonoTouch.Foundation;
 
namespace RoutExcel.Controls
{
/// <summary>
/// A class to show a date picker on an action sheet. To use, create a new ActionSheetDatePicker,
/// set the Title, modify any settings on the DatePicker property, and call Show(). It will
/// automatically dismiss when the user clicks "Done," or you can call Hide() to dismiss it
/// manually.
/// </summary>
[MonoTouch.Foundation.Register("SlideOnDatePicker")]
public class ActionSheetDatePicker
{
#region -= declarations =-
UIActionSheet _actionSheet;
UIButton _doneButton = UIButton.FromType (UIButtonType.RoundedRect);
UIView _owner;
UILabel _titleLabel = new UILabel ();
UITextField _textField;
#endregion
#region -= properties =-
/// <summary>
/// Set any datepicker properties here
/// </summary>
public UIDatePicker DatePicker
{
get { return this._datePicker; }
set { this._datePicker = value; }
}
UIDatePicker _datePicker = new UIDatePicker(RectangleF.Empty);
/// <summary>
/// The title that shows up for the date picker
/// </summary>
public string Title
{
get { return this._titleLabel.Text; }
set { this._titleLabel.Text = value; }
}
#endregion
#region -= constructor =-
public ActionSheetDatePicker(UIView owner, UITextField textField) : this(owner)
{
this._textField = textField;
}
 
protected void SetDatePickerDateFromTextField ()
{
if (_textField != null)
{
DateTime dt;
bool success = DateTime.TryParse(_textField.Text, out dt);
if (success)
{
_datePicker.TimeZone = NSTimeZone.FromAbbreviation("GMT");
_datePicker.Date = MonoTouch.Foundation.NSDate.FromTimeIntervalSinceReferenceDate((date-(new DateTime(2001,1,1,0,0,0))).TotalSeconds);
}
}
}
/// <summary>
///
/// </summary>
public ActionSheetDatePicker (UIView owner)
{
//---- save our uiview owner
this._owner = owner;
//---- configure the title label
this._titleLabel.BackgroundColor = UIColor.Clear;
this._titleLabel.TextColor = UIColor.LightTextColor;
this._titleLabel.Font = UIFont.BoldSystemFontOfSize (18);
//---- configure the done button
this._doneButton.SetTitle ("done", UIControlState.Normal);
this._doneButton.TouchUpInside += (s, e) =>
{
this._actionSheet.DismissWithClickedButtonIndex (0, true);
this._textField.ResignFirstResponder();
};
//---- create + configure the action sheet
this._actionSheet = new UIActionSheet () { Style = UIActionSheetStyle.BlackTranslucent };
this._actionSheet.Clicked += (s, e) => { Console.WriteLine ("Clicked on item {0}", e.ButtonIndex); };
//---- add our controls to the action sheet
this._actionSheet.AddSubview (this._datePicker);
this._actionSheet.AddSubview (this._titleLabel);
this._actionSheet.AddSubview (this._doneButton);
}
#endregion
#region -= public methods =-
/// <summary>
/// Shows the action sheet picker from the view that was set as the owner.
/// </summary>
public void Show ()
{
SetDatePickerDateFromTextField ();
//---- declare vars
float titleBarHeight = 40;
SizeF doneButtonSize = new SizeF (71, 30);
SizeF actionSheetSize = new SizeF (this._owner.Frame.Width, this._datePicker.Frame.Height + titleBarHeight);
RectangleF actionSheetFrame = new RectangleF (0, this._owner.Frame.Height - actionSheetSize.Height
, actionSheetSize.Width, actionSheetSize.Height);
//---- show the action sheet and add the controls to it
this._actionSheet.ShowInView (this._owner);
//---- resize the action sheet to fit our other stuff
this._actionSheet.Frame = actionSheetFrame;
//---- move our picker to be at the bottom of the actionsheet (view coords are relative to the action sheet)
this._datePicker.Frame = new RectangleF
(this._datePicker.Frame.X, titleBarHeight, this._datePicker.Frame.Width, this._datePicker.Frame.Height);
//---- move our label to the top of the action sheet
this._titleLabel.Frame = new RectangleF (10, 4, this._owner.Frame.Width - 100, 35);
//---- move our button
this._doneButton.Frame = new RectangleF (actionSheetSize.Width - doneButtonSize.Width - 10, 7, doneButtonSize.Width, doneButtonSize.Height);
}
/// <summary>
/// Dismisses the action sheet date picker
/// </summary>
public void Hide (bool animated)
{
this._actionSheet.DismissWithClickedButtonIndex (0, animated);
}
#endregion
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.