简体   繁体   中英

Loading UIViewController from storyboard through its custom init method

I have custom init method for my UIViewController , I want to load it from storyBoard as only XYZController() gives blank View.

convenience init() {
    self.init(imageURL: nil)
}

init(imageURL: NSURL?){
    //code to load the ViewController from StoryBoard
    super.init(nibName: nil, bundle: NSBundle.mainBundle())

}

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

If you want to load the view controller from the storyboard, then you're going to have to follow a slightly different pattern.

First, name the segue to your view controller in the storyboard (click on it, then enter a name for 'Identifier' in the properties tab.

When the segue is triggered, prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) will be called. You can make sure you're moving to the appropriate view controller by checking the segue's identifier property, then cast the segue's destinationViewController to you XYZViewController.

After that, you should be able to call a setup function or however you want to supply the image url to the controller.

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