最近碰到一個問題,需求如下:
需求:在某個storyboard中有一個單獨的控制器VC(沒有連線),在storyboard中給此VC隨便拖幾個控件在上面,同時自定義一個類綁定此VC,問如何獲取到此VC并且顯示storyboard中此VC設(shè)置的界面。如下圖:
1,首先要知道是哪個storyboard,先介紹一下storyboard的三個方法:
1> 下面的方法表示通過storyboard的名字從某個包中獲取(一般設(shè)置為nil表示從主包中獲取)storyboard
+ (UIStoryboard *)storyboardWithName:(NSString *)name bundle:(NSBundle *)storyboardBundleOrNil
2> 此方法獲取到storyboard中初始的控制器
- (id)instantiateInitialViewController
3> 此方法表示通過控制器的identifier獲取到此控制器,新版本的Xcode用Storyboard ID來表示某個控制器的identifier
- (id)instantiateViewControllerWithIdentifier:(NSString *)identifier;
好了,從上面的三個方法應(yīng)該很多朋友都知道怎么獲取Storyboard中單獨的控制器了。
2,在storyboard中設(shè)置此單獨的VC的identifier,即Storyboard ID,用于區(qū)分storyboard中的控制器,如下圖
3,在類中調(diào)用storyboard中的-instantiateViewControllerWithIdentifier:方法獲取次單獨的VC
ZJDuLiController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"DuLi"]
4,push或modal出次VC即可
[self.navigationController pushViewController:vc animated:YES];
最終效果如下:
原創(chuàng)文章,轉(zhuǎn)載請注明出處:http://www.companysz.com/Abnerzj/p/3819421.html
新聞熱點
疑難解答