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

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

问题描述

在 UIViewController 的视图中嵌套视图控制器的视图在 iOS 中通常是糟糕的编程实践吗?比如说,我想要某种交互元素来响应用户的触摸,但只占屏幕的 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.

实现容器视图控制器

使用带有 Storyboard 的 Interface Builder 进行设置也非常简单,请查看对象库中的容器视图.

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