Disable Past Date in UIDatePicker in IPhone Object

2019-03-22 10:08发布

I am new to Objective C and IPhone development.

I am using a UIDatePicker in my IPhone Application and my requirement is to allow the user to choose the future date only from the DatePicker.

To do this i want to disable past date & time in DatePicker only instead of putting a compare validation in the code.

Is this possible or there is an option to disable the past date in the DatePicker control itself.

3条回答
我只想做你的唯一
2楼-- · 2019-03-22 10:24

Use the following line,to disable the previous dates of an UIDatePicker.

[picker setMinimumDate: [NSDate date]];

The current date is displayed as shown below

enter image description here

查看更多
▲ chillily
3楼-- · 2019-03-22 10:28

For Swift :

let todaysDate = Date()

datePicker.minimumDate = todaysDate

It will not let the user to pick a date older than Today

OR

Or, if you want to reduce the code, use following way:

datePicker.minimumDate = Date()
查看更多
Melony?
4楼-- · 2019-03-22 10:37

Did you try this

[picker setMinimumDate: [NSDate date]];
查看更多
登录 后发表回答