本文翻译自:Instantiate and Present a viewController in Swift
Issue 问题
I started taking a look of the new Swift
on Xcode 6
, and I tried some demo projects and tutorials. 我开始看看Xcode 6
上的新Swift
,我尝试了一些演示项目和教程。 Now I am stuck at: 现在我被困在:
Instantiating and then presenting a viewController
from a specific storyboard 实例化然后从特定的故事板呈现viewController
Objective-C Solution Objective-C解决方案
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"myStoryboardName" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"myVCID"];
[self presentViewController:vc animated:YES completion:nil];
How to achieve this on Swift? 如何在Swift上实现这一目标?
#1楼
参考:https://stackoom.com/question/1CQRa/在Swift中实例化和呈现viewController
#2楼
It all is a matter of the new syntax, functionality hasn't changed: 这一切都是新语法的问题,功能没有改变:
// Swift 3.0
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "someViewController")
self.present(controller, animated: true, completion: nil)
If you're having problems with init(coder:)
, please refer to EridB's answer . 如果您遇到init(coder:)
,请参考EridB的答案 。
#3楼
For people using @akashivskyy's answer to instantiate UIViewController
and are having the exception: 对于使用@ akashivskyy的答案来实例化UIViewController
并且有异常的人:
fatal error: use of unimplemented initializer 'init(coder:)' for class 致命错误:在课堂上使用未实现的初始化程序'init(coder :)'
Quick tip: 小建议:
Manually implement required init?(coder aDecoder: NSCoder)
at your destination UIViewController
that you are trying to instantiate 在您尝试实例化的目标UIViewController
上手动实现required init?(coder aDecoder: NSCoder)
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
If you need more description please refer to my answer here 如果你需要更多的描述,请参阅我的答案在这里
#4楼
// "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)
This worked fine for me when i put it in AppDelegate 当我把它放在AppDelegate中时,这对我来说很好
#5楼
This link has both the implementations: 此链接具有以下两种实现:
Swift: 迅速:
let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ViewController") as UIViewController
self.presentViewController(viewController, animated: false, completion: nil)
Objective C 目标C.
UIViewController *viewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"];
This link has code for initiating viewcontroller in the same storyboard 此链接具有用于在同一故事板中启动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)
}
#6楼
akashivskyy's answer works just fine! akashivskyy的回答很好! But, in case you have some trouble returning from the presented view controller, this alternative can be helpful. 但是,如果您从显示的视图控制器返回时遇到一些问题,则此替代方法可能会有所帮助。 It worked for me! 它对我有用!
Swift: 迅速:
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: OBJ-C:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MyStoryboardName" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"someViewController"];
[self.navigationController showViewController:vc sender:nil];