Cross Platform
Android
iOS
 

ActionSheet Date Picker

This recipe shows how to pop up an Action Sheet that contains a Date Picker.

Recipe

An Action Sheet is a convenient way to modally request input from the user. This recipe shows you how to use an Action Sheet to allow the user to input a date.

To display an Action Sheet that selects a date:

  • The sample code includes a custom class ActionSheetDatePicker that will be used in this recipe. It contains a custom UIActionSheet implementation that contains a UIDatePicker, UILabel and UIButton.
  • Create the ActionSheetDatePicker and set the Title:
actionSheetDatePicker = new ActionSheetDatePicker (this.View);
actionSheetDatePicker.Title = "Choose Date:";
  • Set the type and validation properties of the UIDatePicker property:
actionSheetDatePicker.DatePicker.Mode = UIDatePickerMode.DateAndTime;
actionSheetDatePicker.DatePicker.MinimumDate = DateTime.Today.AddDays (-7);
actionSheetDatePicker.DatePicker.MaximumDate = DateTime.Today.AddDays (7);
  • Handle the UIDatePicker value changing (in this case, we apply the selected date directly to the UILabel):
actionSheetDatePicker.DatePicker.ValueChanged += (s, e) => {
    dateLabel.Text = (s as UIDatePicker).Date.ToString ();
};
  • In the Choose a date button TouchUpInside handler call Show to display the Action Sheet:
actionSheetDatePicker.Show ();

Additional Information

The sample includes a custom ActionSheetDatePicker class that works by creating a UIActionSheet and adding other subviews directly to it.