views:

1190

answers:

1

I have an app where I ask for the users birthday with a UIDatePicker then store it in standard user defaults. When the app is opened again I get the date again and then want to set the UIDatePicker to that date but it's crashing and not accepting the date. It's being stored in the format "June 8, 2009."

How do I need to do this? This is the way I'm trying it now:

- (void)viewWillAppear:(BOOL)animated {
    birthDate = [Preferences getBirthDate];
    if (birthDate == nil) {
        [datePicker setDate:[NSDate date] animated:NO];
    }    
}

My user pref methods look like this in their class:

+ (NSDate *)getBirthDate {
    // Geting via user defaults....
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"DOB"])
        return [[NSUserDefaults standardUserDefaults] objectForKey:@"DOB"];
    else {
        return nil;
    }
}

+ (BOOL)setBirthDate:(NSDate *)bDate {
    //Setting via user defaults
    [[NSUserDefaults standardUserDefaults] setObject:bDate forKey:@"DOB"];
    return [[NSUserDefaults standardUserDefaults] synchronize];
}

Thanks in advance.

A: 

From your code, you never set the datePicker's date to be birthdate. Try this:

- (void)viewWillAppear:(BOOL)animated {
NSDate *birthDate = [Preferences getBirthDate];
if (birthDate == nil) {
    birthDate = [NSDate date];
}

datePicker.date = birthDate;

}

Dan Lorenc
Thanks! I was trying to use [datePicker setDate:birthDate] to set the date. What does that method do if it doesn't set the date to what you want?
Xcoder
[datePicker setDate:birthDate] is the same thing as datePicker.date=birthDate. The main difference is that if birthDate is not equal to nil in your code, the datePicker's date does not get set.
Dan Lorenc
Yes, I know. I commented it out because [datePicker setDate:birthDate] was crashing it. However, for some reason datePicker.date did not. Very strange.
Xcoder