开发者

Passing an NSDate

开发者 https://www.devze.com 2022-12-24 16:28 出处:网络
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'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 开发者_如何学JAVAseem to work as initialDate is always NULL. What am I doing wrong?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消