Swift:以编程方式创建拆分视图控制器时,UI 元素等于 nil [英] Swift: UI Elements equal nil when programatically creating split view controller

查看:30
本文介绍了Swift:以编程方式创建拆分视图控制器时,UI 元素等于 nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在触摸表视图单元格时使用以下代码以编程方式创建拆分视图控制器:

I am programmatically creating a split view controller using the following code when a table view cell is touched:

let rootViewController: UIViewController = RootTableViewController()
        let navVC: UINavigationController = UINavigationController(rootViewController: rootViewController)

        let detailViewController: UIViewController = DetailTableViewController()

        let splitVC: UISplitViewController = UISplitViewController()
        splitVC.viewControllers = [navVC, detailViewController]
        self.present(splitVC, animated: true, completion: nil)

但是当我点击 tableViewCell 时,我收到错误:致命错误:在解包可选值时意外发现 nil",该值似乎链接到 RootTableViewController 上的 UITextField(和所有 UI 元素).第一个失败是在执行上述代码后RootViewController的viewDidLoad中,当一个值传递给一个ui元素时

but when I tap the tableViewCell I get the error: 'fatal error: unexpectedly found nil while unwrapping an Optional value' which appears to be linked to a UITextField (and all UI Elements) on the RootTableViewController. The first failure is in the viewDidLoad of RootViewController after executing the above code when a value is passed to a ui element

推荐答案

这到底发生在哪里?错误来自哪里?我有一个类似的问题,我在系统创建 IBOutlets 之前尝试访问它们,这导致我的应用程序崩溃.

Where exactly does this happen? Wheres the error originated? I had a similar issue where I tried to access IBOutlets before they were created by the system which caused my app to crash.

特别是我有一个 UI 更新函数,它在设置 ViewController 的属性后被调用.

Specifically I had a UI update function which was called after setting a property of the ViewController.

我通过检查更新函数中的 nil 解决了这个问题,因为它是在第一次调用 viewDidLoad 之前调用的,所以我手动调用了 viewDidLoad 中的更新函数,以确保当它第一次显示时,一切正常正确更新.

I got around this by checking for nil in the update function and since it was called before viewDidLoad was called the first time, I called the update function in viewDidLoad manually to make sure that when it shows for the first time, everything is correctly updated.

更新

我想我知道发生了什么.您创建了一个 Storyboard,设置了您的 UI,并选择了您的 ViewController 作为 Storyboard 中 ViewControllers 的类.

I think I have an idea of whats going on. You created a Storyboard, setup your UI and chose your ViewControllers as the classes of the ViewControllers in the Storyboard.

如果您现在想使用 ViewControllers,您必须通过 Storyboard 实例化它们,而不是手动初始化它们(类似这样):

If you now want to use the ViewControllers, you have to instantiate them via the Storyboard rather than manually initializing them (something like this):

let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "someViewController")

你也可以使用 Segues 而不是实例化一些东西.使用 Storyboard 构建完整的 UI,然后使用 Segue 来呈现 SplitViewController.

You also could use Segues instead of instantiating something at all. Build your complete UI using the Storyboard and then use a Segue to present the SplitViewController.

我能想到的最后一种方法是,如果您想手动实例化 ViewControllers 并且仍然使用 Storyboard 或 nib,则必须在 ViewControllers 的 init 函数中进行一些自定义初始化(此代码来自我在单独的 .xib 中的自定义视图):

The last method I can think of is, that if you want to instantiate the ViewControllers manually and still make use of the Storyboard or a nib, you have to do some custom initialization in in the init functions of your ViewControllers (this code is from a custom view I have in a separate .xib):

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    initializeSubviews()
}

override init(frame: CGRect) {
    super.init(frame: frame)
    initializeSubviews()
}

func initializeSubviews() {
    UINib(nibName: "DatePickerKeyboard", bundle: nil).instantiate(withOwner: self, options: nil)
    addSubview(view)
    view.frame = self.bounds
}

这篇关于Swift:以编程方式创建拆分视图控制器时,UI 元素等于 nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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