zoukankan      html  css  js  c++  java
  • 用代码调用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

  • 相关阅读:
    艾伟_转载:C#来创建和读取XML文档 狼人:
    艾伟_转载:一个.NET委托的故事:彼得,老板和宇宙 狼人:
    艾伟_转载:.NET 4.0 Beta2中的BigInteger和Complex类 狼人:
    艾伟_转载:在C#中实现3层架构 狼人:
    艾伟_转载:从MySpace基于.NET平台的六次重构经历,来感受分布式 狼人:
    艾伟_转载:.NET设计模式:单件模式(Singleton Pattern) 狼人:
    艾伟_转载:.NET重写URL浅谈 狼人:
    Windows phone8 基础篇(二) XAML简介
    TCP/IP详解第十八章
    使用JCS时出现config.OptionConverter Could not instantiate class []的错误原因
  • 原文地址:https://www.cnblogs.com/tanglimei/p/4741998.html
Copyright © 2011-2022 走看看