抱歉说来话长 -
我对正确使用导航 Controller 层次结构有一些疑问。
每个 UIViewController
都有一个属性 self.navigationController
但并不是每个 View Controller 都是从导航 Controller 呈现的——因此该属性可能为 nil。
更是如此 -
一些 View Controller 期望能够做到:
[self.navigationController presentViewController:nextViewController];
但如果它本身不包含在导航 Controller 层次结构中,则该表达式将不执行任何操作。
所以实习生 View Controller 可以测试它是否嵌入在导航 Controller 中:
if (self.navigationController == nil)
{
// create a UINavigationController with a root view controller - the next view controller
UINavigationController *nav = [UINavigationController alloc] initWithRootViewController:nextViewController];
[self presentViewController:nav];
}
但这可能会破坏目的 - 因为如果呈现 View Controller 不在导航 Controller 中 - 为什么下一个 View Controller 一定需要在导航 Controller 中。为什么当前的 View Controller 不这样做:
[self presentViewController:nextViewController];
并跳过导航 Controller 的创建?
确切地说 - 我面临的另一个问题 -
一些 View Controller 希望从 UINavigationController
中呈现 -
因为他们在提供的导航 Controller 栏中设置导航项 - 工具栏项、标题等。
如果出于某种原因他们的 self.navigationController == nil
- 他们如何“将自己”放入导航 Controller 中以确保他们可以访问那些预期的元素???
关于导航 Controller 问题中的容器 View Controller :
如果您查看现有的 iOS 音乐应用程序 - 您会看到似乎是 UITabBarController
嵌入在 UIViewNavigationController
中 - 每个选项卡都是一个单独的 View Controller - 而每个人都可以自己“推送”和“弹出” View Controller 。
我需要实现类似的效果(但布局不同)所以我创建了一个容器 View Controller ,其中包含 UITabBar
View 。
然后我在该容器 View Controller 中创建了一个演示文稿容器
View
这个容器是每个 subview Controller 的 View 所在的地方。(那些是“父 View Controller ”的“ subview Controller ”)——这有点绕口令,对此我深表歉意。
标签栏切换 - 父 View Controller 交换该容器中的可见 View Controller 。
现在每个 View Controller 都非常通用 - 他们不知道它们的包含但希望有一个设置(非零)的 UINavigationController
变量以设置标题 - 按钮 - 导航项等
通用黑盒 View Controller 如何在导航 Controller 中“呈现”自身(将自身嵌入)-
容器 View Controller 如何设置 subview Controller 的 navigationController
属性?
最佳答案
我有一个应用程序,它有一个占位符 (self.mainView),用于根据选项卡选择放置 viewController。其中一些需要 NavigationController,而另一些则不需要。如果 self.navigationController == nil 并相应地工作,你总是可以做一个测试。
这是我用来在占位符中显示 viewController 的一些示例代码,可能会对您有所帮助。
if (self.currentController) {
[self.currentController willMoveToParentViewController:nil];
[[self.currentController view] removeFromSuperview];
[self.currentController removeFromParentViewController];
self.currentController = nil;
}
switch (tag) {
case 0:
self.currentController = [self.storyboard instantiateViewControllerWithIdentifier:@"AboutViewController"];
break;
case 1: {
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"EventsViewController"]];
self.currentController = controller;
}
break;
}
if (self.currentController) {
[self addChildViewController:self.currentController];
[self.mainView addSubview:self.currentController.view];
[self.currentController didMoveToParentViewController:self];
[self adjustMainView];
[self.view layoutIfNeeded];
}
关于ios - View Controller "embed"本身如何在 UINavigationController 中 - 特别是当包含在容器 View Controller 中时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22683238/