简体   繁体   中英

Programmatically segue from MainViewController to .xib view in Swift?

I'm having trouble finding help on performing a segue, or some kind of view transition with swift with at least iOS 8 or later. I'm trying to avoid using the Storyboard editor as much as possible, aside from implementing a segue from the Main View Controller to the xib.

This is what I have so far:

    let bundle = NSBundle(forClass: self.dynamicType)
    let secondViewController = ViewController(nibName: "SwitchRegionSegue", bundle: nil)
    self.presentViewController(secondViewController, animated: true, completion: nil)

The code above is called from the main ViewController.swift using the Single View Application new project scaffold. But it results in a crash:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "SwitchRegionSegue" nib but the view outlet was not set.'

I also have these two files like so:

SwitchRegionSegue.xib

SwitchRegionSegue.swift

I've made sure that the file's owner custom class for the xib is set to SwitchRegionSegue : UIView

My SwitchRegionSegue.swift is just a blank canvas and looks like this:

import UIKit

class SwitchRegionSegue: UIView {

    var view: UIView!

    var nibName = "SwitchRegionSegue"

    func xibSetup() {
        view = loadViewFromNib()

        // use bounds not frame or it'll be offset
        view.frame = bounds

        // Make the view stretch with containing view
        view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
        addSubview(view)
    }

    func loadViewFromNib() -> UIView {

        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: nibName, bundle: bundle)
        let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView

        return view
    }

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

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

}

Anyone know what it is that I'm not doing correctly here? (suggestion to a good swift & xib textbook would be awesome)

In the .xib file, two things must be true:

  • The File's Owner proxy's class is set, in the Identity inspector, to the class of the view controller.

  • The view outlet of the File's Owner proxy must be connected to the view object in the nib.

You have forgotten one of both of those.

Issues that I noticed from you code:

1) Inherit SwitchRegionSegue from UIViewController instead of UIView

2) Set the Class of your xib

3) Connect the view object in the nib to view outlet

Then write this code in your main ViewController:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    let secondViewController = SwitchRegionSegue(nibName: "SwitchRegionSegue", bundle: nil)
    self.presentViewController(secondViewController, animated: true, completion: nil)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM