OGeek|极客世界-中国程序员成长平台

标题: iOS View 层次结构和嵌套导航 Controller [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 02:33
标题: iOS View 层次结构和嵌套导航 Controller

我的应用程序根目录中有 3 个 View Controller / View :LoginViewControllerHomeViewControllerPlayViewcontroller

我正在寻找的是一种基于状态在这三个之间进行交换的好方法(例如:成功登录将导致 loginViewcontroller ping 其委托(delegate) [rootviewcontroller] 以删除自身并显示 homeviewcontroller)

我似乎有几个选择:

1.RootViewController 是一个 UIViewController。它根据需要添加/删除 subview 。

[self.view addSubview:loginViewController.view];

2.RootViewController 是一个 UIViewController。它根据需要以模态方式呈现/删除 View 。

[self presentViewController:loginViewcontroller animated:NO completion:nil];

3.RootViewController 是一个 UINavigationController。它插入/弹出东西。

[self pushViewcontroller:loginViewController];

我读到第一个选项有点解决 View Controller 应该是什么的整个想法,因此并不理想。

第二个选项似乎很奇怪,因为我的整个应用程序都是模态的? View Controller 可以在模态呈现的 View Controller 之上以模态呈现吗?

第三个选项似乎最好,(如 here 所述),但在我的具体实现中遇到了一些问题:

所以,我的问题是:

当其中一些 View Controller 本身是 UINavigationControllers 时,在相同层次结构中构建 View Controller 的最佳方法是什么?

****注意:*** 我已经看过 here ,但它没有为我的用例(多个导航 Controller )提供足够的答案。*******


编辑:我找到了答案:我认为问题是错误的,应该考虑创建自己的 Container ViewController .



Best Answer-推荐答案


我认为您应该考虑结合使用 2 和 3。由于登录是 session 中的一次性操作,因此最好不要让这些 Controller 一直处于事件状态 - 最好先使用它们,然后再使用它们离开。为此,我将使窗口的 Root View Controller 成为导航 Controller ,以 HomeViewController 作为其根,并推送到 PlayViewController 的转换。 LoginViewController 应该从 HomeViewController 中的 viewDidAppear 方法以模态方式呈现,因此它会在应用程序首次打开时出现(如果您关闭了动画)。以模态方式呈现一个导航 Controller 是可以的,它会覆盖你的根导航 Controller ,所以你不会看到两个导航栏。

关于iOS View 层次结构和嵌套导航 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16348704/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4