简体   繁体   English

如何以编程方式在新的故事板中选择到新的ViewController

[英]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. 场景:我有一个完全用代码创建的视图控制器,系统认为我在情节提要A中。我想从此视图控制器中选择到情节提要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. 如果此视图控制器是使用Storyboard创建的,则可以创建附加到Storyboard引用的segue(这是一个很好的建议)。 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. 我的另一个选择是使下一个视图控制器完全用代码创建,这样我就可以不使用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. 这段代码使您可以在应用程序中的任何位置选择任何viewController,同时仍然能够使用情节提要构建您的viewController。

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. 您可以覆盖以下功能以覆盖segue调用。

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

   var destinationController = segue.destinationViewController

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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