Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
608 views
in Technique[技术] by (71.8m points)

uinavigationcontroller - iPhone - Replacing rootview in navigation controller

I have a project with a NavigationController, that contains into IB the first ViewController to show. That's just the default pattern when creating the project.

In that first viewController, I receive some event that I send to the appDelegate, and I want it to replace that rootview with another one. Not going to a next one.

So I do :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
    // Override point for customization after application launch.
    // Add the navigation controller's view to the window and display.
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void) goNext {

    [self.navigationController popViewControllerAnimated:NO];

    NextViewController* nextWindow = [[[NextViewController alloc] initWithNibName:@"NextView" bundle:nil] autorelease];
    [self.navigationController pushViewController:nextWindow animated:NO];
}

But that doesn't work, the first viewcontroller is not poped. And the second one is logically displayed with a back button.

I just want to replace that first view with the other one as the start of the navigation process.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From UINavigationController reference:

This method (popViewControllerAnimated:) removes the top view controller from the stack and makes the new top of the stack the active view controller. If the view controller at the top of the stack is the root view controller, this method does nothing. In other words, you cannot pop the last item on the stack.

You can try the setViewControllers:animated: method instead.

- (void) goNext {

    NextViewController* nextWindow = [[[NextViewController alloc] initWithNibName:@"NextView" bundle:nil] autorelease];
    [self.navigationController setViewControllers:[NSArray arrayWithObject:detailViewController] animated:YES];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...