栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在另一个视图控制器中将视图控制器添加为子视图

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在另一个视图控制器中将视图控制器添加为子视图

一些观察:

  1. 实例化第二个视图控制器时,您正在调用
    ViewControllerB()
    。如果该视图控制器以编程方式创建其视图(这是不寻常的),那就很好了。但是,IBOutlet建议的存在暗示了第二个视图控制器的场景是在
    Interface Builder1z
    中定义的,但是通过调用
    ViewControllerB()
    ,您并没有给情节提要板实例化该场景并连接所有出口的机会。因此,隐式解包的UILabel是nil,导致出现错误消息。
    相反,您想在
    Interface Builder
    中为目标视图控制器提供一个“ storyboard id” ,然后可以使用
    instantiateViewController(withIdentifier:
    )它实例化(并连接
    所有IB插座)。在Swift 3中
    let controller = storyboard!.instantiateViewController(withIdentifier: "scene storyboard id")


You can now access this

controller
‘s
view
.

  1. But if you really want to do
    addSubview
    (i.e. you’re not transitioning to the next scene), then you are engaging in a practice called “view controller containment”. You do not just want to simply
    addSubview
    . You want to do some additional container view controller calls, e.g.:
    let controller = storyboard!.instantiateViewController(withIdentifier: "scene storyboard id")

    addChild(controller)
    controller.view.frame = … // or, better, turn off

    translatesAutoresizingMaskIntoConstraints
    and then define constraints for this subview
    view.addSubview(controller.view)
    controller.didMove(toParent: self)

For more information about why this

addChild

(previously called
addChildViewController
) and
didMove(toParent:)

(previously called
didMove(toParentViewController:)
) are necessary, see
WWDC 2011 video #102 - Implementing UIViewController
Containment. In short,
you need to ensure that your view controller hierarchy stays in sync with your
view hierarchy, and these calls to
addChild
and
didMove(toParent:)
ensure
this is the case.

另请参阅 《View Controller编程指南》中的“ 创建自定义容器View
Controller ”。

顺便说一句,以上说明了如何以编程方式执行此操作。它
实际上是容易得多,如果您使用界面生成器“容器视图”。

在此处输入图片说明

这样,您就不必担心与这些与收容相关的任何呼叫,
Interface Builder会为您解决。

For Swift 2 implementation, see previous revision of this
answer.



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/436375.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号