崩溃:storyboardWithName:

Crashing: storyboardWithName:

使用以下方法获取故事板实例:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"storyboardName" bundle:nil];

我注意到 "If no storyboard resource file matching name exists, an exception is thrown with description: Could not find a storyboard named 'XXXXXX' in bundle...."。我知道处理方法不能太小心

我的问题是有什么方法可以捕获异常并手动处理它。 有什么方向吗?

NSString *sbName = @"storyboardName";
UIStoryboard *sb;
@try {
     sb = [UIStoryboard storyboardWithName:sbName bundle:nil];
}@catch (NSException *exception) {
     [self warnMissingStoryBoard:sbName];
     //handle here...
     return;
}
//use sb here...
UIStoryboard *st;
@try{
st = [UIStoryboard storyboardWithName:@"XXXXXX" bundle:nil];
}@catch (NSException *exception)
{
NSLog (@"%@",[exception description]);
return;

}

在这种情况下,如果 storyboardWithName:@"XXXXXX" 不存在,那么代码将进入 catch 块,在那里我们可以手动进行必要的处理。我刚刚记录了异常的描述。我们可以在该块中进行任何处理而不会导致代码崩溃