UIViewController视图中的iOS嵌套视图控制器视图? [英] iOS Nested View Controllers view inside UIViewController's view?

查看:176
本文介绍了UIViewController视图中的iOS嵌套视图控制器视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS中,在UIViewController的视图中有一个嵌套的视图控制器视图通常是不好的编程习惯吗?比方说,我希望有某种响应用户触摸的互动元素,但只占屏幕的25%。

Is it typically bad programming practice in iOS to have a nested view controller's view inside UIViewController's view? Say for instance I wanted to have some kind of interactive element that responds to user's touches, but only takes up maybe 25% of the screen.

我想我会把这个嵌套的视图控制器添加到我的UIViewController中,例如:

I suppose I would add this nested view controller to my UIViewController by saying something like:

[self.view addSubview: nestedViewController.view];


推荐答案

不,这通常是好的设计,它有助于保持你的视图控制器简洁。但是,您应该使用视图控制器包含模式,请查看以下文档。

No, this is generally good design, it helps keep your view controllers concise. However you should be using the view controller containment pattern, take a look at the following documentation.

实现容器视图控制器

使用Interface Builder和Storyboard进行设置非常简单,看一下对象库中的Container View。

This is incredibly simple to setup using Interface Builder with Storyboards as well, take a look at the Container View in the object library.

这是一个故事板中的一个人为的例子。在这个例子中,你将有4个视图控制器,一个容纳3个容器,每个容器一个。当您呈现包含所有容器的最左侧控制器时,Storyboard将自动初始化并嵌入另一个3.您可以通过 childViewControllers 属性访问这些子视图控制器或有一种方法可以覆盖 prepareForSegue:sender:并捕获要调用的segue的目标视图控制器。如果需要,还可以将属性传递给子视图控制器。

Here is a contrived example in a Storyboard. In this example you would have 4 view controllers, one that holds the 3 containers, and one for each container. When you present the left most controller that has all of the containers, the Storyboard will automatically initialize and embed the other 3. You can access these child view controllers via the childViewControllers property or there is a method you can override prepareForSegue:sender: and capture the destination view controllers of the segue about to be called. This is also a good point to pass properties to the child view controllers if any are needed.

这篇关于UIViewController视图中的iOS嵌套视图控制器视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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