·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> 获取Storyboard中单独的控制器

获取Storyboard中单独的控制器

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

最近碰到一个问题,需求如下:

需求:在某个storyboard中有一个单独的控制器VC(没有连线),在storyboard中给此VC随便拖几个控件在上面,同时自定义一个类绑定此VC,问如何获取到此VC并且显示storyboard中此VC设置的界面。如下图:

 

1,首先要知道是哪个storyboard,先介绍一下storyboard的三个方法:

1> 下面的方法表示通过storyboard的名字从某个包中获取(一般设置为nil表示从主包中获取)storyboard
+ (UIStoryboard *)storyboardWithName:(NSString *)name bundle:(NSBundle *)storyboardBundleOrNil

2> 此方法获取到storyboard中初始的控制器
- (id)instantiateInitialViewController 

3> 此方法表示通过控制器的identifier获取到此控制器,新版本的Xcode用Storyboard ID来表示某个控制器的identifier
- (id)instantiateViewControllerWithIdentifier:(NSString *)identifier; 

好了,从上面的三个方法应该很多朋友都知道怎么获取Storyboard中单独的控制器了。

 

2,在storyboard中设置此单独的VC的identifier,即Storyboard ID,用于区分storyboard中的控制器,如下图

                 

 

3,在类中调用storyboard中的-instantiateViewControllerWithIdentifier:方法获取次单独的VC

ZJDuLiController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"DuLi"]

 

4,push或modal出次VC即可

[self.navigationController pushViewController:vc animated:YES];

最终效果如下:

                              

 

原创文章,转载请注明出处:http://www.cnblogs.com/Abnerzj/p/3819421.html