简体   繁体   中英

How to Programmatically Segue to a New ViewController in a New Storyboard

I simply want to know how to segue to a new view controller in a new storyboard without having to create the new view controller programmatically.

The Scenario: I have one view controller that's created entirely in code and the system thinks I'm in Storyboard A. I want to segue from this view controller to another view controller that's contained on Storyboard B.

  1. I could create a segue attached to a storyboard reference (which is a great suggestion) if this view controller was created with Storyboard. But it's in code, so I can't do this.
  2. My other option is to make the next view controller be totally created in code so that I can present it without using segues. That's a pain, but will work.
  3. My third option is to initialize the new storyboard in code, initialize the new view controller in code using a storyboard identifier, and then use the navigation controller to segue to it.

If there are other options I'm not aware of please include them below!

This piece of code allows you to segue to any viewController anywhere in your application while still being able to build your viewController with storyboard.

func settingsButtonPressed(sender:UIButton) {
    let storyboard = UIStoryboard(name: "AccountLinking", bundle: nil)
    let linkingVC = storyboard.instantiateViewControllerWithIdentifier("AccountLinkingTable")
    self.navigationController?.pushViewController(linkingVC, animated: true)
}

So many hours saved thanks to this little function.

我敦促所有阅读此文章的人阅读Xcode 7中引入的Storyboard Reference ,以实现此目的,而不是通过编程方式加载Storyboard。

You can override the below function to override the segue call.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

   var destinationController = segue.destinationViewController

}

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