我创建了i navigationController项目名称:autoload, 然后创建两个uiviewContorller命名为:second,three
我希望该进程在方法“viewDidLoad”中加载rootView加载第二个然后在方法“viewdidload”中自动加载三个, 这是代码:
rootView:
- (void)viewDidLoad {
self.title = @"first";
Second *second = [[Second alloc] init];
AutoLoadAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.navigationController pushViewController:second animated:YES];
[super viewDidLoad];
}
第二
- (void)viewDidLoad {
self.title = @"second";
[super viewDidLoad];
}
现在建立一个去程序,我可以自动加载到第二个非常正确 包括标题内容和导航按钮
然后我想要aotoload三秒钟,所以在第二种方法中添加代码:“viewdidload”
第二
- (void)viewDidLoad {
self.title = @"second";
**Three *three = [[Three alloc] init];
AutoLoadAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.navigationController pushViewController:three animated:YES];**
[super viewDidLoad];
}
最后将标题添加到三个:
3:
- (void)viewDidLoad {
self.title = @"three";
[super viewDidLoad];
}
然后构建并去,你会发现内容是正确的“三” 但标题错了“第二”,应该是“三” 并且您还会发现导航按钮错误
我的错误是什么,我应该怎么做才能将自动加载程序实现为三个?
请注意:
我试试: 如果我在第二个添加按钮并移动代码
Three *three = [[Three alloc] init];
AutoLoadAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.navigationController pushViewController:three animated:YES];
到ibaction,它将正常工作,但我希望它自动加载
答案 0 :(得分:2)
尝试在applicationDidFinishLaunching方法中设置viewControllers数组,而不是在viewDidLoad方法中调用pushViewController:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
RootViewController *root = [[RootViewController alloc] init];
root.title = @"root";
Second *second = [[Second alloc] init];
second.title = @"second";
Three *three = [[Three alloc] init];
three.title = @"three";
[navigationController setViewControllers:[NSArray arrayWithObjects:root,second,three,nil] animated:YES];
[root release];
[second release];
[three release];
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}