In my rootTableViewCobtroller
, I want make a generic method . When I select cell,rootTableViewCobtroller
push one of all of my other ViewControllers (named certain rules)
When I select cell , do push action:
-(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];
}
So my other ViewControllers's class name is List1,list2,List3...
Qusetion:
In the above code ,I just initial viewcontroller using code .For some reason ,for a few viewcontroller, I use storyboard file associated with it .The storyboard file ,corresponding to each other named it's storyboard id is same as class name
e.g:
List1 not has a storyboard file,I should creat List1 instance through
List1 *list1VC = [[List1 alloc]init];
List2 not has a storyboard file,I should creat List2 instance through
List2 *list2VC = [[List2 alloc]init];
List3 has a storyboard file ,name it's storyboard file's storyboard id List3
.I should creat List1 instance through
List3 *list3VC = [self.storyboard instantiateViewControllerWithIdentifier:@"List3"];
So I should modify my generic method like this:
-(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];
}
But how to check a storyboard id exists or not, If I donot check whether a class has associated storyboard file just use
[self.storyboard instantiateViewControllerWithIdentifier:string]
while crash my app.