在SWIFT中实例化并呈现viewController

在SWIFT中实例化并呈现viewController

发行

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

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

目标-C溶液

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

如何在SWIFT上实现这一点?


至尊宝的传说
浏览 1130回答 3
3回答

慕森王

这都是新语法的问题,功能没有改变:// Swift 3.0let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)let controller = storyboard.instantiateViewController(w ithIdentifier: "someViewController")self.present(controller, animated: true, completion: nil)如果你有问题init(coder:),请参阅EridB回答.

慕神8447489

实例化UIViewController并且有例外:致命错误:类使用未实现的初始化器‘init(编码器:)’快速提示:手动实现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 UIViewControllerself.presentViewController(viewController, animated: false, completion: nil)目标CUIViewController *viewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@" 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)}
打开App,查看更多内容
随时随地看视频慕课网APP