iosobjective-cnsdatepicker

NSDatePicker returns default date even when date is set programatically


I have a datePicker instance which is linked to the IBOutlet.

@interface SomeClass() {

    __weak IBOutlet UIDatePicker *datePicker;
}

Now in my awakeFromNib method I am setting the minimumDate, maximumDate and date property

- (void)awakeFromNib {

    [datePicker setMinimumDate:[[NSDate date] dateByAddingTimeInterval:60*10]];
    [datePicker setMaximumDate:[[NSDate date] dateByAddingTimeInterval:60*60*24*10]];
    [datePicker setDate:datePicker.minimumDate];

    NSLog(@"%@", datePicker.date);
    //Logs Correct Value
}

When I log datePicker.date after setting it in awakeFromNib method, it gives correct value. Till now so good.

Now when I try to get the date value after the view is initialized, for ex, on a button click event, it gives me the default value which is current date value.

- (IBAction)aButtonTouchUpInsideEvent:(UIButton *)button {
    NSLog(@"%@", datePicker.date);
    //Logs [NSDate date] value
}

I want to know why it is not giving me the value which I set in awakeFromNib method. I tried searching for this issue but no luck.


Solution

  • You value is overridden at some time by system. You can move your code to -willMoveToSuperview: