开发者

minimumDate and maximumDate of UIDatePicker cannot be set in code?

开发者 https://www.devze.com 2023-02-21 13:50 出处:网络
In my NIB file, I have a UIDatePicker. I connect it to a IBOutlet property. In the - (id) initWithCoder:(NSCoder *)aDecoder method in my Controller, I set the minimumDate and开发者_C百科 maximumDate l

In my NIB file, I have a UIDatePicker. I connect it to a IBOutlet property. In the - (id) initWithCoder:(NSCoder *)aDecoder method in my Controller, I set the minimumDate and开发者_C百科 maximumDate like this:

- (id) initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];  
        [dateFormatter setDateFormat:@"yyyy-MM-dd"]; 
        NSDate *beginDate = [dateFormatter dateFromString:@"2010-01-01"];  
        NSDate *endDate = [dateFormatter dateFromString:@"2010-12-31"]; 
        picker.minimumDate = beginDate;
        picker.maximumDate = endDate;
    }
    return self;
}

But when the app is running, there is no change about my UIDatePicker. Did I have some mistakes?

Thanks!


Your picker binding is not done in the initWithCoder method, as the XIB file is not loaded at this stage.

Place you code in awakeFromNib

- ( void )awakeFromNib
{
   ...
}

And by the way, you forgot to release your NSDateFormatter object...

0

精彩评论

暂无评论...
验证码 换一张
取 消