views:

111

answers:

2

I have a uiviewcontroller with two properties: trackName and playerObject. PlayerObject also has a trackName property. I call this uiviewcontroller from my main uiviewController with this code:

SecondaryViewController *nextViewController = [[SecondaryViewController alloc] initWithNibName:@"SecondaryViewController" bundle:nil];
NSString *trackName = @"a track";
nextViewController.trackName = trackName;
[self.navigationController pushViewController:nextViewController animated:YES];
[nextViewController release];

In SecondaryViewController I override the initwithnibname method to set the trackName of the playerObject. I do this with this code:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
    playerObject.trackName = trackName;
}
playerObject.trackName = trackName;
return self;

}

Finally my playerObject has all of the view data the SecondaryViewController will need. It looks like:

- (void)awakeFromNib{
NSString *s = trackName;
    //more code relevant to the the view controller
}

When I debug, the trackName string in the playerObject is nil. I assume I'm doing something wrong. How can I have this value populated with the trackName I originally passed in the main uiview controller?

A: 

It seems like when you are initing the viewController the playerObject variable has not yet been set, could this be possible?

Daniel
Daniel, can I set the trackName in another method besides initwithnibname in the secondaryviewcontroller? If so which method?
Buffernet
I dont know for what purpose you are using this t rackName, you can maybe set it in view did load or something of the sort? You must set it after you assign the player object tho
Daniel
the secondaryviewcontrol doesn't do the loading. Everything to do with the view is done in the player object class. trackname is irrelevant I just need to set a property in the object from the original mainviewclass.
Buffernet
A: 

This can sometimes happen when you override initWithNibName:bundle:.

Instead use viewDidLoad to do setup. Apple guarantees all required setup is performed before this method is called (not the case with initWithNibName:bundle:).

Corey Floyd