UINavigationController not added correctly when initiated by viewDidLoad [duplicate]
Possible Duplicate:
Call function on "viewDidLoad" [SOLVED]
I have function, startApp, which looks like this:
- (void)startApp {
NSLog(@"startApp initiated");
aNavController = [[UINavigationController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:aNavController animated:YES]; }
When called by a button, like:
- (IBAction)showSettings:(id)sender {
[self startApp]; }
It works fine, but called by viewDidLoad, like:
- (void)viewDidLoad {
[super viewDidLoad];
[self startApp]; }
"startApp initiated" is outputted but the NavigationController does not appear. I have no idea why this is, it seems to that they should work the same way? Being in the same file and all. Does anyone 开发者_Python百科have any idea why this isn't working properly?
Thank you, Tobias Tovedal
write this method in viewdidLoad [self performSelector:@selector(loadView) withObject:nil afterDelay:1]; now create one method - (void) loadView { //write your navigation code here }
In viewDidLoad you are attempting to cover up an existing view with a navigation view. That would work with most view controllers, but I think the UINavigationViewController wants to be the root or bottom-most view controller in your app.
Perhaps it would work to instantiate your navigation view controller, then create the view you want to appear within it.
It's tricky to use navigation controllers directly from code. You'd have a much easier time if you used Xcode's File->New Project to create a Navigation-Based App, then added whatever existing code you have to that.
you cannot present a view controller in viewDidLoad method. try it out in viewDidAppear.
精彩评论