您的位置:首页 > 产品设计 > UI/UE

iOS UI篇11- Storyboard(快速实例化)

2016-06-24 15:22 525 查看
Storyboard Name和identifier如果不对,将会抛出异常。怎么避免这个问题呢?

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

使用UIViewController的类别方法

使用要求:

Storyboard中的ViewController的类名和Storyboard ID相同。

类别的头文件:

#import <UIKit/UIKit.h>
@interface UIViewController (Storyboard)

+ (nullable instancetype)dd_instanceFromStoryboardWithIdentifier:(nonnull NSString *)identifier;

+ (instancetype)dd_instanceFromStoryboard;

@end


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

使用方式 :

YouViewController *vc = [YouViewController dd_instanceFromStoryboard];


具体实现步骤:

检查缓存里面有没有保存这个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;
}


使用示例:

- (IBAction)btnAction:(id)sender
{
DDTestVC *aVC = [DDTestVC dd_instanceFromStoryboard];
[self.navigationController pushViewController:aVC animated:YES];
}

@end


GitHub地址:https://github.com/developforapple/DDStoryboard
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios ui