【问题标题】:UIDatePicker setMinimumDate and setMaximumDate sets nullUIDatePicker setMinimumDate 和 setMaximumDate 设置为空
【发布时间】:2011-09-09 16:22:05
【问题描述】:

我不知道为什么这段极其基本的代码会设置空值。

NSDate *currentTime = [NSDate date];
[datePicker setMinimumDate:currentTime];
[datePicker setMaximumDate:[currentTime dateByAddingTimeInterval:kSecondsInYear]];
NSLog(@"cur: %@, min: %@, max: %@",currentTime,datePicker.minimumDate,datePicker.maximumDate);

输出:当前:2011-09-09 16:18:18 GMT,最小值:(null),最大值:(null)

【问题讨论】:

  • 你检查过 datePicker 不是 nil 吗?记录 datePicker 以检查它的设置是否正常。

标签: iphone ios nsdate uidatepicker


【解决方案1】:

鉴于以下工作正常,问题肯定出在 datePicker 上。

UIDatePicker* datePicker = [[UIDatePicker alloc] init];
NSLog(@"DatePicker: %@", datePicker);
NSDate *currentTime = [NSDate date];
[datePicker setMinimumDate:currentTime];
[datePicker setMaximumDate:[currentTime dateByAddingTimeInterval:40000]];
NSLog(@"cur: %@, min: %@, max: %@",currentTime,datePicker.minimumDate,datePicker.maximumDate);

如果您将NSLog(@"DatePicker: %@", datePicker); 添加到您的代码 sn-p 中,它应该会报告一些类似于:DatePicker: <UIDatePicker: 0x4b36740; frame = (0 0; 320 216); layer = <CALayer: 0x4b33940>> 的内容。如果它返回 (null)(我非常怀疑),那么它要么不是以编程方式创建的,要么没有连接到 Interface Builder。

【讨论】:

  • 感谢您的努力,但我最终回答了自己的问题。
【解决方案2】:

发现了问题,我在 initwithnib 方法中设置了最小/最大日期。当我将其更改为 viewDidLoad 方法时,它工作正常。哎呀。

【讨论】:

  • 关于 maxDate 有一个有趣的细节:超出范围的日期仍然可见,但显示为深灰色(禁用),而不是隐藏。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-24
  • 2015-04-09
  • 1970-01-01
相关资源
最近更新 更多