通过Storyboard搭建app框架,以及设计和实现一些view controller和view,是非常方便和高效的。
有时,同样一个scene(i.e. view controller),除了在Storyboard上通过segue达到以外,还需要在代码的某个地方,让它展现出来。
如何在代码中,调用一个已经在storyboard中设计好的scene呢?代码如下:
1 2 3 4 5 |
|
注:代码段来自Call storyboard scene programmatically (without needing segue)?
有两个注意的地方:
- 上面代码中的storyboardName不要包括”.storyboard”后缀。即,如果你的Storyboard文档叫做“Main.storyboard”,那么storyboardName应该叫@“Main”
- 注意:在Storyboard中,先给你的viewController加上ID,添加的地方如下图所示:在Indentity中的Storyboard ID。通过这个viewControllerID(图中的例子就是@“UserGuide”),在代码中找到这个vc。
关键字:代码中调用storyboard中的vc,代码中present storyboard scene