我有一个带有两个属性的uiviewcontroller:trackName和playerObject。 PlayerObject还具有trackName属性。我用我的主uiviewController用这段代码调用这个uiviewcontroller:
SecondaryViewController *nextViewController = [[SecondaryViewController alloc] initWithNibName:@"SecondaryViewController" bundle:nil];
NSString *trackName = @"a track";
nextViewController.trackName = trackName;
[self.navigationController pushViewController:nextViewController animated:YES];
[nextViewController release];
在SecondaryViewController中,我重写initwithnibname方法来设置playerObject的trackName。我用这段代码做到了这一点:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
playerObject.trackName = trackName;
}
playerObject.trackName = trackName;
return self;
}
最后,我的playerObject拥有了SecondaryViewController所需的所有视图数据。它看起来像:
- (void)awakeFromNib{
NSString *s = trackName;
//more code relevant to the the view controller
}
调试时,playerObject中的trackName字符串为nil。我想我做错了什么。如何使用我最初在主uiview控制器中传递的trackName填充此值?
答案 0 :(得分:0)
似乎在启动viewController时尚未设置playerObject变量,这可能吗?
答案 1 :(得分:0)
当您覆盖initWithNibName时,有时会发生这种情况:bundle:。
而是使用viewDidLoad进行设置。 Apple保证在调用此方法之前执行所有必需的设置(不是initWithNibName:bundle :)的情况。