views:

144

answers:

1

I'm new to the iPhone and objective-c and I am still trying to get my head around ownership of objects so bear with me.

I have a DOBViewController that has a datepicker on it. I have created a custom init method:

-(id)initWithInitialDate(NSDate *)initialDate;

Another view controller has a date instance declared (NSDate *dob) that holds a value. I would like to pass this to the DOBViewController so that it can scroll the datepicker to the passed value.

Here is the custom init method:

- (id)initWithInitialDate:(NSDate *)initialDate {
// Initialise ourselves
self = [super init];

// Set the date of the date picker
if (initialDate != nil) {
    datePicker.date = initialDate;
}
else {
    // Set it to today
    datePicker.date = [NSDate date];
}

return self;

}

This is how I am sending the dob NSDate object:

DOBViewController *dobViewController = [[DOBViewController alloc] initWithInitialDate:dob];

Where dob is a variable of type NSDate that is declared in the header file.

This doesn't seem to work as initialDate is always NULL. What am I doing wrong?

+1  A: 

Your datePicker outlet isn't loaded yet by the time the init... method returns. You should save the NSDate into an instance variable, and then do the setting in a more appropriate method, like viewDidLoad.

Dave DeLong
Perfect. Thanks!
Garry