如何检查故事板ID是否存在

时间:2017-03-11 05:27:18

标签: ios objective-c

在我的rootTableViewCobtroller中,我想制作一个通用的方法。当我选择单元格时,rootTableViewCobtroller推送我所有其他ViewControllers中的一个(命名为某些规则)

当我选择单元格时,请执行推送操作:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath{
    NSString *string = [NSString stringWithFormat:@"List%ld",indexPath.row+1];
    Class vcClass = NSClassFromString(string);
    UIViewController *vc = [[vcClass alloc]init];
    vc.view.backgroundColor = [UIColor whiteColor];
   [self.navigationController pushViewController:vc animated:YES];

}

所以我的其他ViewControllers的类名是List1,list2,List3...

Qusetion:

在上面的代码中,我只是使用代码初始viewcontroller。出于某种原因,对于一些viewcontroller,我使用与之关联的storyboard文件。故事板文件,彼此对应命名它的故事板id是相同的作为班级名称

e.g:

List1没有故事板文件,我应该通过

创建List1实例
 List1 *list1VC =    [[List1 alloc]init];

List2没有故事板文件,我应该通过

创建List2实例
List2 *list2VC =   [[List2 alloc]init];

List3有一个故事板文件,将它命名为storyboard文件的故事板标识List3。我应该通过

创建List1实例
 List3 *list3VC =   [self.storyboard instantiateViewControllerWithIdentifier:@"List3"];

所以我应该像这样修改我的通用方法:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
    (NSIndexPath *)indexPath{
    NSString *string = [NSString stringWithFormat:@"List%ld",indexPath.row+1];
    Class vcClass = NSClassFromString(string);
    UIViewController *vc;

    //**if storyboard contain a file with storyboard id is equal to "string"**
    if ([self.storyboard contain_storyboard_id:  string]){
       vc = [self.storyboard instantiateViewControllerWithIdentifier:string];
    }else{
   vc = [[vcClass alloc]init];
    vc.view.backgroundColor = [UIColor whiteColor];
   [self.navigationController pushViewController:vc animated:YES];
  }

但是如何检查故事板ID是否存在,如果我不检查类是否有关联的故事板文件只是使用 崩溃我的应用时[self.storyboard instantiateViewControllerWithIdentifier:string]

0 个答案:

没有答案
相关问题