ios - 一个 ViewController 可以包含多个父(即包含) Controller 吗?

标签 ios objective-c ios5 uiviewcontroller uinavigationcontroller

标题非常清楚地提出了问题..但这里有一些上下文可以使事情变得更清楚:

我正在开发电子邮件客户端 app将 UINavigationController 作为根 VC 附加到主窗口(您可以在UI 工作方式下看到 UI 层次结构 here):

    HomeViewController *homeController = [[HomeViewController alloc] initWithNibName:@"HomeView" bundle:nil];
    UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:homeController];
    [self.window addSubview:navController.view];

稍后它将 View Controller 推送到导航 Controller ,即文件夹 View ->邮箱 View ->邮件 View 等:

[self.navigationController pushViewController:someController animated:YES];

我想实现sliding view controller effect在邮件 View 上。

问题是滑动 View Controller 效果实现为 Custom Container View Controller .假设您通过启动包含 View Controller (让我们称之为 ECSlidingViewController )(它是窗口 Root View Controller )来初始化应用程序。然后向其中添加顶部 View Controller (即显示的第一个 View Controller ) :

ECSlidingViewController *slidingViewController = (ECSlidingViewController *)self.window.rootViewController;

slidingViewController.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstTop"];

然后附加应该出现在顶 View Controller 下的 View Controller (slidingViewController 是且始终是本示例中唯一包含的 Controller ):

- (void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];

  if (![self.slidingViewController.underLeftViewController isKindOfClass:[MenuViewController class]]) {
    self.slidingViewController.underLeftViewController  = [self.storyboard instantiateViewControllerWithIdentifier:@"Menu"];
  }

  [self.view addGestureRecognizer:self.slidingViewController.panGesture];
  [self.slidingViewController setAnchorRightRevealAmount:280.0f];
}

我想弄清楚的是..鉴于我的邮件 View 在技术上是 UINavigationController 的 subview Controller ,难道这个所谓的ECSlidingViewController也可以是 child 吗? ? b/c 我希望它包含在由导航 Controller 作为父级/控制的导航 Controller 中,并且我希望它上下滑动..这意味着它必须ECSlidingViewController .

此外,假设 VC 可以有多个父级......在上面示例的上下文中......我应该在 View 层次结构中的哪里添加该父级?我应该将其添加为 UINavigationController 的子级吗?这是否符合最佳实践(假设有一些)?

最佳答案

简短回答

不.. View Controller 不能包含多个父 Controller ..这在UIView类引用insertSubview:aboveSubview的描述中几乎明确地提到了。 : View 只能有一个 super View 。如果 View 已经有一个 super View 并且该 View 不是接收者,则此方法会在使接收者成为新的 super View 之前删除先前的 super View 。 由于每个 View Controller 都直接与一个 View 关联,因此从上面可以看出, View Controller 不能有多个父 Controller 。

长答案

经过大量研究和使用代码..事实证明我对问题的看法是错误的..长话短说..自定义容器 View Controller 遵循适用于预构建容器的相同规则像 UINavigation 和 UITab 这样的 Controller ..并且在任何时候都可以将自定义容器作为另一个 Controller 的子级包含在内,反之亦然..这一点得到了最好的总结 here :

容器 Controller 既可以是其他 Controller 的父级,也可以是另一个容器的子级。最终,这种 Controller 组合建立了 View Controller 层次结构。

因此,如果您想知道是否应该将两个父级附加到同一个 View Controller 。那么您可能应该重新考虑您的 View 层次结构。b/c 这种情况不应该发生。在上述问题的上下文中..我应该让 UINavigationController 成为我的自定义包含 Controller 的子级..并且因为我有 flexibility of communication仅当选择 Mailview 时,我才能使 UINavigation 上下滑动。

关于ios - 一个 ViewController 可以包含多个父(即包含) Controller 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14235099/

相关文章:

ios - iOS 8 应用程序/框架中缺少必需的模块 'CocoaLumberjack'

iOS:如何检查流连接是否超时

ios - 将文本移动到 UITableViewCell 中的下一个文本字段

objective-c - 在本地和远程之间切换 iOS 应用程序中使用的 url

iphone - 具有不同页面宽度的 UIScrollview

uitableview - 确定表格单元格何时开始在可能的移动操作中拖动(移动)

ios - 执行 segue 有时间限制吗?

iphone - 安装 Xcode 4.2 后,命令/usr/bin/codesign 失败,退出代码为 1

objective-c - iOS 6 通讯录清空 kABPersonPhoneProperty

iphone - AVCaptureDeviceOutput 不调用委托(delegate)方法 captureOutput