问题
i create i navigationController project name:autoload, then create two uiviewContorller named: second,three
i want the process is load rootView the load second in method:"viewDidLoad" then auto load three in method"viewdidload", here is the code:
rootView:
- (void)viewDidLoad {
self.title = @"first";
Second *second = [[Second alloc] init];
AutoLoadAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.navigationController pushViewController:second animated:YES];
[super viewDidLoad];
}
second:
- (void)viewDidLoad {
self.title = @"second";
[super viewDidLoad];
}
now build an go the program, i can auto load to second very correct includeing the title content and also the navgation button
then i want aotoload three in second,so add the code in second method:"viewdidload"
second:
- (void)viewDidLoad {
self.title = @"second";
**Three *three = [[Three alloc] init];
AutoLoadAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.navigationController pushViewController:three animated:YES];**
[super viewDidLoad];
}
finaly add the title to three:
three:
- (void)viewDidLoad {
self.title = @"three";
[super viewDidLoad];
}
then build and go, you will find that the content is right "three" but the title is wrong "second", it should be "three" and you will also find the navgation button is wrong
what's wrong with my that,what i should do to implement the program of autoload to three?
note:
i try that : if i add a button in second and move the code
Three *three = [[Three alloc] init];
AutoLoadAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.navigationController pushViewController:three animated:YES];
to the ibaction ,it will be work correct, but i want it load automaticly
回答1:
Instead of calling pushViewController in the viewDidLoad methods, try setting the viewControllers array in the applicationDidFinishLaunching method:
- (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];
}
来源:https://stackoverflow.com/questions/2360578/iphone-navigationcontroller-autoloading-to-three-level