如何在导航控制器之间切换?

发布于 2024-09-01 19:20:44 字数 394 浏览 0 评论 0原文

情况:
我有一个基于“Navigation-Based-Application”模板的 Xcode 项目。所以这意味着我有一个管理 UIViewController 的单个 UINavigationController。

我想做什么:
我想要做的就是向我的项目中再添加一个 UINavigationController —— 并能够在它们之间来回切换。 (我想这样做是为了为一些单独的不相关内容腾出空间,这样它就没有后退按钮指向根视图控制器。)

问题:
如何在我的项目中再添加一个 UINavigationController 并在两个 UINavigationController 之间切换?

Situation:
I have an Xcode project based on the "Navigation-Based-Application" template. So that means I have a SINGLE UINavigationController that manages a UIViewController.

What I want To Do:
What I want to do is add one more UINavigationController to my project -- and be able to switch back and forth between them. (I want to do this to make space for some seperate unrelated content so that it does not have a back button pointing back to the root view controller.)

Question:
How do I add one more UINavigationController to my project and switch between the two UINavigationControllers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

命比纸薄 2024-09-08 19:20:44

最常见、最自然的 iPhone 操作系统方法是将 UITabBarController 添加到您的应用程序中。 Xcode 模板标签栏应用程序将指导您正确使用它。

但是...

如果您不喜欢在应用程序中使用选项卡栏,并且希望在不同的 UINavigationController 实例(或任何与此相关的 UIViewController)之间切换,你可以做这样的事情。

首先,您需要在适当的位置创建 UINavigationController 实例(例如,一个新的视图控制器,或者如果您想采取简单的方法,则在您的应用程序委托中)。然后,您只需交换应该可见的导航控制器的视图即可在控制器之间切换。

应用程序委托中的示例,“firstNavigationController”和“secondNavigationController”是 UINavigationController 实例变量:

- (void)showFirstNavigationController {
    [secondNavigationController.view removeFromSuperview];
    [self.window addSubview:firstNavigationController.view];
}

这将仅显示第一个而不是第二个导航控制器。请注意,这个示例非常简单。我没有考虑到您应该正确处理 viewWillAppear:viewDidAppear: 等方法。

The most common, and natural iPhone OS, way of doing this is to add a UITabBarController to your application. The Xcode template Tab Bar Application will guide you in the right direction on how to use it.

But...

If you don't like to have a Tab Bar in your application, and wish to switch between different UINavigationController instances (or any UIViewController for that matter), you can do something like this.

First you need to create your UINavigationController instances in a appropriate place (for example a new view controller, or in you Application Delegate, if you want to take the easy way out). You can then switch between controllers by just swapping which Navigation Controller's view that should be visible.

Example in the Application Delegate, "firstNavigationController" and "secondNavigationController" are UINavigationController instance variables:

- (void)showFirstNavigationController {
    [secondNavigationController.view removeFromSuperview];
    [self.window addSubview:firstNavigationController.view];
}

This will simply display the first instead of the second Navigation Controller. Note that this example is very simple. I didn't take into consideration that you should correctly handle the methods viewWillAppear:, viewDidAppear: and so on.

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