storyboard 如何用代码调用

转自网络:

 

1、得到一个storyboard对象(UIStoryBoard类的对象)。

如果你有一个已经存在的view controller,而且这个view controller是从同一个storyboard实例化而来的,那么可以通过它的storyboard属性获得storyboard对象。要加载另外一个storyboard对象,可以调用UIStoryboard的类方法storyboardWithName:bundle: ,并将storyboard文件的文件名和一个可选的bundle作为参数传给该方法。

 

2、调用storyboard对象的instantiateViewControllerWithIdentifier: 方法,将你在Interface Builder中创建view controller时定义的标识符传给这个方法。

或者,你可以使用instantiateInitialViewController方法来实例化storyboard的首个view controller,而不用指定标识符。

 

3、显示这个新的view controller。

==============

一下用代码解释更清楚:

==============

如何在代码中,调用一个已经在storyboard中设计好的scene呢?代码如下:

NSString * storyboardName = @"MainStoryboard_iPhone";
NSString * viewControllerID = @"ViewID";
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
MyViewController * controller = (MyViewController *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID];
[self presentViewController:controller animated:YES completion:nil];
posted @ 2014-10-28 14:59  memorecool  阅读(631)  评论(0编辑  收藏  举报