iPhone - 替换导航控制器中的 rootview

发布于 2024-11-23 20:19:59 字数 952 浏览 5 评论 0原文

我有一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别挽留 2024-11-30 20:19:59

来自 UINavigationController参考

此方法 (popViewControllerAnimated:) 从堆栈中删除顶部视图控制器并使
新的堆栈顶部是活动视图控制器。如果视图
堆栈顶部的控制器是根视图控制器,这
方法什么也不做。换句话说,您无法弹出最后一项
堆栈。

您可以尝试使用 setViewControllers:animated: 方法。

- (void) goNext {

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

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];
}
暮色兮凉城 2024-11-30 20:19:59

如果堆栈顶部的视图控制器是根视图控制器,则 popViewControllerAnimated 不执行任何操作。换句话说,您无法弹出堆栈中的最后一项。

要替换根视图控制器,请使用以下方法:

- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated

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:

- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated
请叫√我孤独 2024-11-30 20:19:59

在斯威夫特中:

self.navigationController?.setViewControllers([detailViewController], animated: true)

In Swift:

self.navigationController?.setViewControllers([detailViewController], animated: true)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文