Swift 3
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "viewController")
self.navigationController!.pushViewController(vc, animated: true)
Swift 2
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("viewController")
self.navigationController!.pushViewController(vc, animated: true)
先决条件
给您的视图控制器分配一个Storyboard ID。
![故事板ID](https://istack.dev59.com/2ga56.webp)
IB > 显示 Identity inspector > Identity > Storyboard ID
Swift(旧版)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("viewController") as? UIViewController
self.navigationController!.pushViewController(vc!, animated: true)
编辑:根据Fred A.的评论,建议使用Swift 2。
如果你想在没有任何导航控制器的情况下使用,你必须像下面这样使用:
let Storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = Storyboard.instantiateViewController(withIdentifier: "viewController")
present(vc , animated: true , completion: nil)
[sb instantiateInitialViewController]
是一个方便的方法。 - drewishself.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
2)self.window.rootViewController = vc;
并且3)[self.window makeKeyAndVisible];
。因为这不是从应用程序委托的模态转换,所以您还可以去掉modalTransitionStyle
行。 - lewiguez