Loading ViewController from xib file

前端 未结 10 2414
我在风中等你
我在风中等你 2020-12-08 02:33

I had a MyViewController.swift and a MyViewController.xib presenting the layout of MyViewController.

I tried different methods to load this

10条回答
  •  佛祖请我去吃肉
    2020-12-08 02:38

    Notice the File's Owner. In your case, the File's Owner must be MyViewController, or its sub-class.

    And the following code, if it executes in class Foo.

    // If `self` is an instance of `Foo` class.
    // In this case, `File's Owner` will be a `Foo` instance due to the `self` parameter.
    let myVC = NSBundle.mainBundle().loadNibNamed("MyViewController", owner: self, options: nil)[0] as? MyViewController
    

    It assigns self as owner. So, the File's Owner is Foo, not MyViewController. Then, for Foo class, those IBOutlet cannot be connected to Foo. So, it throws exception.

提交回复
热议问题