samedi 21 février 2015

Xcode - Check if NSDate is nil inside an if/else statement's argument

I have a UIDatePicker that sets the chosen NSDate in a variable (selectedDate). When the view controller with the date picker loads, I want it to show the date that was selected instead of today's date. However, if no date's been selected yet, it should show today's date.


I'm not sure how to setup the if/else statement's argument. This is what I have, but it causes my app to crash. The idea is to change the UIDatePicker's to whatever the user previously selected only if _selectedDate is not empty, or doesn't have a value of nil.


.h



@property (nonatomic, strong)IBOutlet UIDatePicker *datePicker;
@property (nonatomic, strong)NSDate *selectedDate;


.m



- (void)viewDidLoad {
if (_selectedDate != nil) {
// Load saved value for _selectedDate
_selectedDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"selectedDate"];
// Set datePicker's date to whatever was selected
[datePicker setDate:_selectedDate];
}
}

Aucun commentaire:

Enregistrer un commentaire