在Swift中实例化和呈现一个viewController

问题

我开始看看Xcode 6上的新Swift ,并尝试了一些演示项目和教程。 现在我被困在:

实例化,然后从一个特定的故事板呈现一个viewController

Objective-C解决scheme

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"myStoryboardName" bundle:nil]; UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"myVCID"]; [self presentViewController:vc animated:YES completion:nil]; 

如何在Swift上实现这一点?

这一切都是新语法的问题,function并没有改变:

 // Swift 3.0 let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil) let controller = storyboard.instantiateViewController(withIdentifier: "someViewController") self.present(controller, animated: true, completion: nil) 

如果你遇到init(coder:) ,请参考EridB的答案 。

对于使用@ akashivskyy的答案来实例化UIViewController人有exception:

致命错误:对类使用未实现的初始化程序'init(coder :)'

快速提示:

手动实现required init?(coder aDecoder: NSCoder)在您的目标UIViewController ,你正试图实例化

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

如果你需要更多的描述,请参考我的答案

这个链接有两个实现:

迅速:

 let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ViewController") as UIViewController self.presentViewController(viewController, animated: false, completion: nil) 

目标C

 UIViewController *viewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"]; 

这个链接有在同一个故事板中启动viewcontroller的代码

 /* Helper to Switch the View based on StoryBoard @param StoryBoard ID as String */ func switchToViewController(identifier: String) { let viewController = self.storyboard?.instantiateViewControllerWithIdentifier(identifier) as! UIViewController self.navigationController?.setViewControllers([viewController], animated: false) } 

akashivskyy的答案工作得很好! 但是,如果您从呈现的视图控制器返回一些麻烦,这种替代方法可能会有所帮助。 它为我工作!

迅速:

 let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil) let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController // Alternative way to present the new view controller self.navigationController?.showViewController(vc, sender: nil) 

OBJ-C:

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MyStoryboardName" bundle:nil]; UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"someViewController"]; [self.navigationController showViewController:vc sender:nil]; 

如果你想以模态的方式呈现,你应该像下面这样:

 let vc = self.storyboard!.instantiateViewControllerWithIdentifier("YourViewControllerID") self.showDetailViewController(vc as! YourViewControllerClassName, sender: self) 
 // "Main" is name of .storybord file " let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) // "MiniGameView" is the ID given to the ViewController in the interfacebuilder // MiniGameViewController is the CLASS name of the ViewController.swift file acosiated to the ViewController var setViewController = mainStoryboard.instantiateViewControllerWithIdentifier("MiniGameView") as MiniGameViewController var rootViewController = self.window!.rootViewController rootViewController?.presentViewController(setViewController, animated: false, completion: nil) 

当我把它放在AppDelegate中时,这对我来说很好

如果你有一个ViewController不使用任何storyboard / Xib,你可以像下面的调用一样推送到这个特定的VC:

  let vcInstance : UIViewController = yourViewController() self.present(vcInstance, animated: true, completion: nil) 

我知道这是一个旧的线程,但我认为目前的解决scheme(使用给定的视图控制器的硬编码string标识符)是非常容易出错。

我已经创build了一个构build时间脚本(您可以在这里访问 ),这将创build一个编译器安全的方式来访问和实例化给定项目中所有storyboard的视图控制器。

例如, Main.storyboard中名为vc1的视图控制器将被如下实例化:

 let vc: UIViewController = R.storyboard.Main.vc1^ // where the '^' character initialize the controller 

Swift 3 故事板

 let settingStoryboard : UIStoryboard = UIStoryboard(name: "SettingViewController", bundle: nil) let settingVC = settingStoryboard.instantiateViewController(withIdentifier: "SettingViewController") as! SettingViewController self.present(settingVC, animated: true, completion: { })