8

I need to set the initial date to something other than "Today" so the user is forced to make a selection. I have found that if the user does NOT make a selection, the date/time is set to "nil"; I can send an UIAlertView, but that is after the fact.

I have looked at SO and Google, but found nothing. How can I do this?

UPDATE: actually, I didn't state it, but the year is not shown, only month, day and am/pm. Without the year, they might not see a future date. T

4

4 回答 4

9

一个问题,你怎么知道用户不会选择你设置的特定日期?

我会用

[datePicker setDate:[NSDate date]];

在日期选择器上显示今天的日期。

于 2013-01-04T20:40:51.700 回答
7

By doing this

[datePicker setDate:yourDate];

in viewDidLoad method.

于 2013-01-04T20:29:13.783 回答
1

您可以设置未来的日期,然后还设置最小日期,以便用户只能选择未来的日期(如果这是您要查找的日期)。

以编程方式

如果您以编程方式执行所有这些操作,则应使用以下属性:

@property(nonatomic, retain) NSDate *maximumDate;
@property(nonatomic, retain) NSDate *minimumDate;
@property(nonatomic, retain) NSDate *date;

使用界面生成器

如果您通过 xib(或情节提要)文件进行操作,则在属性检查器中您可以设置所有属性:日期,还有最小和最大日期,因此如果您希望用户不能将今天设置为日期,但只有未来的日期,使用最小日期:

在此处输入图像描述

PS:如果您只想显示部分日期,请更改格式。

于 2013-01-04T20:42:38.390 回答
1
NSDate *todayDate = [NSDate date];

NSDate *aGo = [todayDate dateByAddingTimeInterval:-6*24*60*60];
  1. 它将为日期选择器设置最小日期

    [_datePickerView setMinimumDate:aGo];

  2. 当我们打开 datepickerview 时,这里设置了默认的日期位置

    [_datePickerView setDate:[NSDate date]];

于 2016-10-24T12:52:45.793 回答