addChildViewController 和 addSubview 的区别? [英] Difference between addChildViewController and addSubview?

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

问题描述

这两种方法都将视图添加为父视图的子视图,并且视图可以接收事件.什么时候使用哪个?

Both the methods add the view as child of parentview and view can receive events. When to use which one?

推荐答案

它们非常不同.addChildViewController 将视图控制器与父容器视图控制器相关联,而 addSubview 将视图添加到视图的视图层次结构中.在前一种情况下,当新的子视图控制器是其父视图控制器时,它将负责处理事件.想想标签栏控制器——每个标签都有自己关联的子"视图控制器,它在父标签栏控制器的内容区域中显示其视图,并在标签栏中选择相应标签时处理该视图中的任何用户交互.当您拥有自定义容器视图并希望将新视图控制器添加到其 childViewControllers 属性时,您应该只使用 addChildViewController.如果您只是想向可以接收事件的视图层次结构中添加一个新视图,这听起来像是这样,addSubview 是要走的路.实现容器视图控制器"部分解释了 addChildViewController 的用途.

They are very different. addChildViewController associates a view controller with a parent container view controller, while addSubview adds a view to the view hierarchy of the view it is being added to. In the former case, the new child view controller will be responsible for handling events when it is the selected view controller of its parent. Think of a tab bar controller--each tab has its own associated "child" view controller that displays its view within the parent tab bar controller's content area and handles any user interaction within that view when its corresponding tab is selected in the tab bar. You should only use addChildViewController when you have a custom container view and want to add a new view controller to its childViewControllers property. If you just want to add a new view to the view hierarchy that can receive events, which is what it kind of sounds like, addSubview is the way to go. "Implementing a Container View Controller" section explains what addChildViewController is for.

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

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