addChildViewController 和 PresentViewController
iOS 5
引入了自定义容器视图控制器的概念,并提供了addChildViewController
等API。问题:您可以将视图控制器添加为子视图控制器,并仍然使用 presentViewController
来呈现它吗?执行后者是否会自动使其成为 PresentingViewController 的子视图控制器?
iOS 5
introduces the concept of custom container view controller and provides API like addChildViewController
. Question: can you add a view controller as a child and still present it using presentViewController
? Does doing the latter automatically make it a child view controller of the presentingViewController
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是它应该如何使用的。
父/子关系适用于当视图控制器具有由其自己的视图控制器管理的子视图时,例如 UITabBarController,其中父视图控制器绘制选项卡,子视图控制器绘制每个选项卡的内容。
如果您使用presentViewController呈现视图控制器,它通常会占据整个屏幕,或者出现在模式中,以便呈现视图控制器不再受控制。在这种情况下,演示者没有理由成为父级,因为它不需要与所呈现的控制器合作 - 它只是避开,直到再次关闭所呈现的控制器。
你为什么要这么做?如果只是为了让视图控制器相互引用并可以传递数据,那么还有其他方法可以实现这一点(例如委托模式、NSNotifications,甚至只是链接两者的属性)。
That's not how it's supposed to be used.
The parent/child relationship is for when a view controller has subviews that are managed by their own view controllers, for example a UITabBarController, where the parent view controller draws the tabs and the child view controllers draw the content of each tab.
If you present a view controller using presentViewController, it generally takes over the whole screen, or appears in a modal so that the presenting view controller is no longer in control. In that scenario there's no reason for the presenter to be the parent because it doesn't need to cooperate with the presented controller - it just gets out of the way until the presented controller is dismissed again.
Why is it that you wanted to do this? If it's just so that the view controllers have a reference to one another and can pass data, there are other ways to do this (e.g. the delegate pattern, NSNotifications, or even just a property linking the two).