How to use one UIDatePicker to display only time on one button click and only date on another?

StackOverflow https://stackoverflow.com/questions/22497320

Im working on an app where in a single view controller I have a date button and a time button. I want to put one date picker in the view. I want that single date picker to display only date on the date button click and only time on the time button click. How is this possible?

有帮助吗?

解决方案

Please set your mode using following property as per your requirement :

@property(nonatomic) UIDatePickerMode datePickerMode

Change your mode in your button actions appropriately.

For example, lets say you have UIDatePicker *datePicker;

you can set mode using below code :

datePicker.datePickerMode = UIDatePickerModeTime;

其他提示

Use either of these approaches.

  • If you are using Storyboard approach, you can simply set the mode to Time.
  • If you are using the code based approach

NSDatePicker *datePicker = [[NSDatePicker alloc] init]; datePicker.datePickerMode = UIDatePickerModeTime; ...

It's very simple.

There is one property in UIDatePicker:

@property (nonatomic) UIDatePickerMode datePickerMode;

Use this property to change picker type.

Picker type are as follows:

typedef NS_ENUM(NSInteger, UIDatePickerMode) {
    UIDatePickerModeTime,           // Displays hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. 6 | 53 | PM)
    UIDatePickerModeDate,           // Displays month, day, and year depending on the locale setting (e.g. November | 15 | 2007)
    UIDatePickerModeDateAndTime,    // Displays date, hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. Wed Nov 15 | 6 | 53 | PM)
    UIDatePickerModeCountDownTimer, // Displays hour and minute (e.g. 1 | 53)
};
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top