iPhone - 替换导航控制器中的 rootview
我有一个带有 NavigationController 的项目,其中包含 IB 中要显示的第一个 ViewController。这只是创建项目时的默认模式。
在第一个 viewController 中,我收到一些发送到 appDelegate 的事件,我希望它用另一个 rootview 替换该 rootview。不会去下一个。
所以我这样做:
- (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];
}
但这不起作用,第一个视图控制器没有弹出。 第二个逻辑上显示有后退按钮。
我只想将第一个视图替换为另一个视图,作为导航过程的开始。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自 UINavigationController参考:
您可以尝试使用
setViewControllers:animated:
方法。From UINavigationController reference:
You can try the
setViewControllers:animated:
method instead.如果堆栈顶部的视图控制器是根视图控制器,则 popViewControllerAnimated 不执行任何操作。换句话说,您无法弹出堆栈中的最后一项。
要替换根视图控制器,请使用以下方法:
If the view controller at the top of the stack is the root view controller, popViewControllerAnimated does nothing. In other words, you cannot pop the last item on the stack.
To replace the root view controller use this method:
在斯威夫特中:
In Swift: