addChildViewController 和 presentViewController [英] addChildViewController and presentViewController

查看:19
本文介绍了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?

推荐答案

这不是应该的使用方式.

That's not how it's supposed to be used.

父/子关系适用于视图控制器具有由其自己的视图控制器管理的子视图,例如 UITabBarController,其中父视图控制器绘制选项卡,子视图控制器绘制每个选项卡的内容.

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.

如果你使用 presentViewController 呈现一个视图控制器,它通常会占据整个屏幕,或者出现在一个模态中,这样呈现的视图控制器就不再受控制.在这种情况下,presenter 没有理由成为父级,因为它不需要与所呈现的控制器合作——它只是让开,直到所呈现的控制器再次被解除.

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.

您为什么要这样做?如果只是为了让视图控制器相互引用并且可以传递数据,那么还有其他方法可以做到这一点(例如委托模式、NSNotifications,甚至只是一个链接两者的属性).

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).

这篇关于addChildViewController 和 presentViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆