Radu Paise Radu Paise - 1 year ago 126
Objective-C Question

empty NSDatePicker

Is it possible to have a NSDatePicker representing a nil object? Something like -/-/- or any other way.


Answer Source

Short answer: No

NSDatePicker simply ignores invalid dates and nil.
For example, this does not update the NSDatePicker, the old value remains:

[datePicker setDateValue:nil];

Only solution: Detect nil and do something specific:

if(date == nil) {

    // Set Specific Date
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyyMMdd"];
    NSDate *nilDate = [dateFormat dateFromString:@"20000101"];  
    [datePicker setDateValue:nilDate];

    // Or Disable
    [datePicker setEnabled:FALSE];

} else {

    // Update Date
    [datePicker setDateValue:date];

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download