Loading ViewController from xib file

前端 未结 10 2393
我在风中等你
我在风中等你 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.

    0 讨论(0)
  • 2020-12-08 02:39

    The problem is not with the methods...you have probably kept an outlet(XXX) connected for some uielement and have removed it from corresponding controller...I am adding example below...

    the above button is connected to controller now but when i comment outlet

    my app crashes

    so try to find outlet(xxx) that is missing from viewcontroller but is in xib file.Hope it helps :)

    0 讨论(0)
  • 2020-12-08 02:39

    Try below code,

    //1

    let nib = UINib(nibName: "MyViewController", bundle:nil)
    myVC = nib.instantiateWithOwner(self, options: nil)[0] as? MyViewController
    

    OR

    myVC = nib.instantiateWithOwner(self, options: nil).first as? MyViewController
    

    //2

    let nib : NSArray = NSBundle.mainBundle().loadNibNamed("MyViewController", owner: self, options: nil)
    myVC = nib.objectAtIndex(0) as? MyViewController
    

    This will work.

    0 讨论(0)
  • 2020-12-08 02:40

    Swift 3

    let myViewController = MyViewController(nibName: "MyViewController", bundle: nil)
    self.present(myViewController, animated: true, completion: nil)
    

    or push in navigation controller

    self.navigationController!.pushViewController(MyViewController(nibName: "MyViewController", bundle: nil), animated: true)
    
    0 讨论(0)
  • 2020-12-08 02:46

    Connect the UIButton with an @IBAction and add the following code to the action method to present a new UIViewController that is set up inside a .xib file.

    @IBAction func handleButtonAction(_ sender: UIButton) {
        let viewControllerInXib = ViewControllerInXib(nibName: "ViewControllerXibName", bundle: nil)
        present(viewControllerInXib, animated: true)
    }
    

    To navigate via UINavigationController you should use this method:

    @IBAction func handleButtonAction(_ sender: UIButton) {
        let viewControllerInXib = ViewControllerInXib(nibName: "ViewControllerXibName", bundle: nil)
        if let navigationController = navigationController {
            navigationController.pushViewController(viewControllerInXib, animated: true)
        } else {
            print("Navigation controller unavailable! Use present method.")
        }
    }
    
    0 讨论(0)
  • 2020-12-08 02:52

    @AechoLiu's answer is great. I had the same question and answered it with the below fix.

    Problem:

    let vc1 = NSViewController(nibName: YDNibIdentifier.myplainvc, bundle: nil)

    Fix:

    let vc1 = MyPlainViewController(nibName: YDNibIdentifier.myplainvc, bundle: nil)

    I had accidentally cast my Nib file to the wrong Clas ( NSViewController ), despite having it connected correctly inside the .xib file.

    0 讨论(0)
提交回复
热议问题