【问题标题】:Inherit ViewController from Storyboard从 Storyboard 继承 ViewController
【发布时间】:2020-03-09 11:33:28
【问题描述】:

我有一个名为 CustomComponents.storyboard 的 Storyboard,我在 Storyboard 中设计了 BaseViewController。例如,我在 Interface Builder 中将 BaseViewController 的 backgroundColor 设置为 .green。我从BaseViewController 继承RootViewController。我希望RootViewController 的backgroundColor 是.green。

这是我的RootViewController

class RootViewController: BaseViewController {

}

这是我的BaseViewController

class BaseViewController: UIViewController {

}

两个类都是空的,我刚刚在 IB 中设置了backgroundColor。

但是,当我运行应用程序时,我看到了白色背景。但是,当在BaseViewController 中以编程方式设置颜色时,绿色背景颜色将按预期应用到RootViewController。

继承在 Storyboard 中设计的 BaseViewController 我缺少什么?基本上,我想在 Storyboard 中设计我的BaseViewController。我做错了什么?

【问题讨论】:

标签: ios swift inheritance uiviewcontroller


【解决方案1】:

只有当您的应用程序最小规格是 iOS 13 时,您才能以编程方式执行此操作,因为新的 API 可用于此目的,因为故事板不包含 类 声明,它是编码实例的存档

因此,如果您的 BaseViewController 具有标识符,您可以将 RootViewController 实例化为编码父为

let rootController = UIStoryboard(name: "CustomComponents", 
    bundle: nil).instantiateViewController(identifier: 
                             "your_base_controller_identifier_here") { coder in
        RootViewController(coder: coder)
}

或者如果你的控制器是 root

let rootController = UIStoryboard(name: "CustomComponents", 
     bundle: nil).instantiateInitialViewController { coder in
        RootViewController(coder: coder)
}

【讨论】:

    猜你喜欢
    • 2012-04-13
    • 1970-01-01
    • 2016-01-19
    • 2022-11-09
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 2013-11-04
    相关资源
    最近更新 更多