I have no idea why this extremely basic piece of code sets null values.
NSDate *currentTime = [NSDate date];
[datePicker setMinimumDate:currentTime]开发者_如何学运维;
[datePicker setMaximumDate:[currentTime dateByAddingTimeInterval:kSecondsInYear]];
NSLog(@"cur: %@, min: %@, max: %@",currentTime,datePicker.minimumDate,datePicker.maximumDate);
Output: cur: 2011-09-09 16:18:18 GMT, min: (null), max: (null)
Given that the following works fine, the issue is definitely with 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);
If you add NSLog(@"DatePicker: %@", datePicker);
to your code snippet it should report something a bit like: DatePicker: <UIDatePicker: 0x4b36740; frame = (0 0; 320 216); layer = <CALayer: 0x4b33940>>
. If it returns (null)
(which I very strongly suspect) then it's either not created programmatically or it's not hooked up in Interface Builder.
Found the problem, I was setting the min/max date in the initwithnib method. When I changed it to the viewDidLoad method it worked fine. whoops.
精彩评论