星空网 > 软件开发 > 操作系统

用代码调用Storyboard里面的viewController

  今天在帮助群里的一个朋友弄pop事件,在他那边,当前的viewcontroller,不能pop出去。

  初步估计,他的ViewController层级多,他自己没有理清。

  因为pushViewController的时候,是执行栈入的原则,先进后出,后进先出。

  比如A->B->C->D,他这个时候,想popB,但是B的上面还有两个,分别为C和D,C和D没有出来,这个时候,B就没有办法出来。所以,即使执行了pop语句,也达不到pop的效果。

  

  然后中午我自己用storyboard拖了几个东西,简单实现了这个效果,发现自己对storyboard还是不太熟,要仔细实践啊!

 

  1.  调出storyboard方法:
UIStoryboard * mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

Parameters

name

The name of the storyboard resource file without the filename extension. This method raises an exception if this parameter is nil.

storyboardBundleOrNil

The bundle containing the storyboard file and its related resources. If you specify nil, this method looks in the main bundle of the current application.

Return Value

A storyboard object for the specified file. If no storyboard resource file matching name exists, an exception is thrown with description: Could not find a storyboard named 'XXXXXX' in bundle....

Discussion

Use this method to retrieve the storyboard object containing the view controller graph you want to access. All of the resources associated with the storyboard must be in the bundle indicated by the storyboardBundleOrNil parameter.


  • Name则是自己起的,在.storyboard前面的名字,如果不动,则默认是Main,而且不需要添加文件的扩展名,如果加了,就会报错;
  • bundle:这个参数包含storyboard的文件以及和它相关的资源,如果为空,则会调用当前程序的main bundle

这样,就能拿到程序里面的storyboard了。

 

  2.调出里面的viewcontroller

 

  我是在里面直接拖拽了几个viewcontroller,当需要加载这几个viewcontroller的时候,有3件事情要做

    2.1 新建ViewController的.h和.m文件,并且将storyboard里面的nib文件的class,也指向对应的VC;

    2.2 设置Storyboard ID,在Custom Class下面,有一个Identity,这个设置标识符,我们就可以找到它了,像Cell的标识符,一般和头文件名称设置为一样就可以。

    2.3 在代码里面调用它:

UIStoryboard * mainStoryBoard =[UIStoryboard storyboardWithName:@"Main" bundle:nil];DViewController * dVC = [mainStoryBoard instantiateViewControllerWithIdentifier:@"DViewController"];[self.navigationController pushViewController:dVC animated:YES];

  这样就可以了。功能就基本实现了。

  

熟能生巧--欢迎大家加入我们的交流群:461093715




原标题:用代码调用Storyboard里面的viewController

关键词:ie

ie
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流