这一切都是新语法的问题,功能没有改变:
// 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的回答.
这一切都是新语法的问题,功能没有改变:
// 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的回答.
Swift 4.2更新的代码是
let storyboard = UIStoryboard(name: "StoryboardNameHere", bundle: nil) let controller = storyboard.instantiateViewController(withIdentifier: "ViewControllerNameHere") self.present(controller, animated: true, completion: nil)
此链接具有以下两种实现:
迅速:
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];
对于使用@ akashivskyy的答案进行实例化UIViewController
并且有异常的人:
致命错误:在课堂上使用未实现的初始化程序'init(coder :)'
小建议:
required init?(coder aDecoder: NSCoder)
在您的目的地手动实施您UIViewController
尝试实例化的内容
required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) }
如果你需要更多的描述,请参阅我的答案在这里
// "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中时,这对我来说很好
如果你想以模态方式呈现它,你应该有类似下面的东西:
let vc = self.storyboard!.instantiateViewControllerWithIdentifier("YourViewControllerID") self.showDetailViewController(vc as! YourViewControllerClassName, sender: self)