addChildViewController和presentViewController [英] addChildViewController and presentViewController

查看:94
本文介绍了addChildViewController和presentViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS 5 引入了自定义容器视图控制器的概念,并提供了类似 addChildViewController 的API。问题:您是否可以将视图控制器添加为子视图并使用 presentViewController 进行显示?做后者会自动使它成为 presentsViewController的子视图控制器吗?

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呈现视图控制器,它通常接管整个屏幕,或以模态显示,以便呈现视图控制器不再受控制。在那种情况下,没有理由让演示者成为父母,因为它不需要与呈现的控制器合作 - 它只是让位于所呈现的控制器再次被解雇之前。

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天全站免登陆