美文网首页iOS-VendoriOS技能收集iOS开发的正确姿势
项目包含多个storyboard文件的情况下,快速取出实例

项目包含多个storyboard文件的情况下,快速取出实例

作者: wwwbbat | 来源:发表于2015-11-17 12:35 被阅读1999次

storyboard可谓Xcode神器,比xib不知道高到哪里去了,也是apple一直推荐使用的。但是随之而来的是两个问题:一是,多人开发时需要同时修改storyboard很容易导致文件冲突,二是,storyboard文件里控制器太多,电脑打开时特别卡和慢。我想这两个问题是导致目前很多开发者选择xib而不是storyboard的原因把。所以在iOS9以后出现了 Storyboard Reference。有点跑题。

一个解决办法是,根据项目功能的划分,建立多个storyboard,各个模块互不影响。那么如何从许多个storyboard文件中快速取出我想要的那个控制器呢?下面应该是大家使用的方法

UIStoryboard *sb = [UIStoryboard storyboardWithName:storyboardName bundle:[NSBundle mainBundle]];

UIViewController *vc = [sb instantiateViewControllerWithIdentifier:identifier];

关键是,storyboardName和identifier如果不对,将会抛出异常。而且,app内部那么多跳转,老是写这样的代码,不觉得烦么?

本文讨论的是如何从多个storyboard中取出控制器实例。不用管控制在哪个storyboard文件里,只要 控制器设置identifier为类名就OK。

使用UIViewController的类别方法

#import <UIKit/UIKit.h>
@interface UIViewController (Storyboard)
+ (nullable instancetype)instanceFromStoryboardV2;
@end

使用 instanceFromStoryboardV2 取出以调用者类名为identifier的实例即可。如果没有取到,返回nil

YouViewController *vc = [YouViewController instanceFromStoryboardV2];

具体实现步骤:

首先:检查缓存里面有没有保存这个identifier对应的storyboard名字。如果有缓存,直接从这个storyboard里面取
接着:获取NSBunble的storyboard文件列表。筛选出storyboard文件名
第三:遍历这个列表,尝试取出实例。
最后:获得实例后对storyboard名进行缓存,同时返回实例
+ (nullable instancetype)instanceFromStoryboardV2
{
NSString *identifier = NSStringFromClass([self class]);

    // 取缓存的storyboard名
    NSCache *cache = [self cache];
    NSString *cacheStoryboardName = [cache objectForKey:identifier];
    if (cacheStoryboardName) {
        return [self tryTakeOutInstanceFromStoryboardNamed:cacheStoryboardName identifier:identifier];
    }

    // 未缓存,遍历storyboard文件名列表,开始尝试取出实例。
    for (NSString *name in [self storyboardList]) {
        UIViewController *instance = [self tryTakeOutInstanceFromStoryboardNamed:name identifier:identifier];
        if (instance) {
            // 成功获取实例后,对storyboard名进行缓存
            [cache setObject:name forKey:identifier];
        return instance;
        }
    }
    return nil;
}

取出项目的storyboard文件列表

storyboard在NSBunble中是以storyboardc为后缀的。所以只要从NSBunble中查找所有storyboardc的文件就可以啦。
需要注意的是,xcode会额外自动生成一个带 ~iPhone 和 ~iPad 的storyboards文件。我们只需要storyboard文件名,所以这两种文件我们需要忽略掉。

+ (nonnull NSArray*)storyboardList
{
     static NSArray *kBundleStoryboardNameList;
     static dispatch_once_t onceToken;
     dispatch_once(&onceToken, ^{
         NSMutableArray *tmp = [NSMutableArray array];

    /**
    *  找到所有storyboard文件。
    *  @warning 会忽略带有 ~iphone(iPhone应用)或 ~ipad(ipad应用)标志的 storyboard文件名
    */
        NSArray *list = [NSBundle pathsForResourcesOfType:@"storyboardc" inDirectory:[NSBundle mainBundle].resourcePath];
        for (NSString *path in list) {
            NSString *ext = [path lastPathComponent];
            NSString *name = [ext stringByDeletingPathExtension];
            if ([name rangeOfString:@"~"].location == NSNotFound) {

                [tmp addObject:name];
            }
          }

      kBundleStoryboardNameList = [NSArray arrayWithArray:tmp];
    });
    return kBundleStoryboardNameList;
}

尝试取出实例

UIStoryboard的+storyboardWithName: bundle:方法如果name不正确,会抛出异常
-instantiateViewControllerWithIdentifier: 方法如果identifier在当前UIStoryboard找不到,也会抛出异常。如果不做处理,会导致app崩溃。
所以这里采用了 try catch 对异常进行捕获。抛出异常时,直接返回nil。
+ (nullable instancetype)tryTakeOutInstanceFromStoryboardNamed:(nonnull NSString *)storyboardName identifier:(nonnull NSString *)identifier
{
if (!storyboardName || !identifier) {
return nil;
}

    @try {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:storyboardName bundle:[NSBundle mainBundle]];
        UIViewController *vc = [sb instantiateViewControllerWithIdentifier:identifier];
        return vc;
    }
    @catch (NSException *exception) {
        return nil;
    }
     @finally {
        
    }  
}

缓存

+ (NSCache *)cache
{
    static NSCache *cache;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        cache = [[NSCache alloc] init];
    });
    return cache;
}

相关文章

  • 项目包含多个storyboard文件的情况下,快速取出实例

    storyboard可谓Xcode神器,比xib不知道高到哪里去了,也是apple一直推荐使用的。但是随之而来的是...

  • 多个Storyboard

    app中包含有多个Storyboard故事模版集合。 Eg:LaunchScreen.storyboard(启动屏...

  • 动态修改合并后的Manifest文件

    APK文件只能包含一个AndroidManifest.xml文件,但是Android Studio项目可以包含多个...

  • 菜鸟教程——Storyboard、nib、xib

    storyboard是多个xib文件集合的描述文件,也采用xml格式。 那么storyboard与xib比较,区别...

  • 速学 storyBoard

    一、storyBoard的介绍 storyBoard又叫故事版 优势:快速开发、项目结构清晰、便于新人接手项目 缺...

  • IDEA中的项目结构

    idea中以项目为单位,一个项目包含多个模块,一个模块又包含多个package,一个包下面可以有多个java源代码文件。

  • 综合使用

    通过storyboard创建 把项目中使用的图片放到Assets.xcassets中,即使包含文件夹在使用时也不用...

  • golang 项目的基本布局结构

    一个 Go 项目可能包含多个具有不同包名的文件。在这些情况下设置适当的布局在 Go 中并不总是那么简单,尤其是对于...

  • 关于storyboard

    storyboard文件的认识 用来描述软件界面 默认情况下,程序一启动就会加载Main.storyboard 加...

  • 小码哥笔记(第一节)

    storyboard文件的认识 用来描述软件界面 默认情况下,程序一启动就会加载Main.storyboard 加...

网友评论

  • 建古:刚好需要 但不懂identifier 哪里来··这边会报错···
  • 建古:NSString *cacheStoryboardName = [cache objectForKey:identifier]; // 这边的identifier哪里来···
  • superD:机智
  • 空灵叔叔:UIStoryboard *storyboardA = [UIStoryboard storyboardWithName:@"StoryboardA" bundle:nil];
    DDTestVC_A_1 *vc = [storyboardA instantiateViewControllerWithIdentifier:@"DDTestVC_A_1"];
    [self.navigationController pushViewController:vc animated:YES];

    我没搞懂像楼主的写法和我上面的代码有什么区别 或者是 楼主的代码有什么优势,谢谢
    wwwbbat:@Enter____ 你这个指定了一个storyboard名以及viewController的id。但是可能项目中使用了很多个storyboard。这样你还得去找这个viewController是在哪个storyboard中创建的,不就麻烦了么? 而且这三行代码需要重复写很多次。
  • 酸三角:不错 学习了

本文标题:项目包含多个storyboard文件的情况下,快速取出实例

本文链接:https://www.haomeiwen.com/subject/rookhttx.html