iOS-VendoriOS技能收集iOS开发的正确姿势

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

2015-11-17  本文已影响1999人  wwwbbat

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;
}
上一篇下一篇

猜你喜欢

热点阅读