0

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.

Community
  • 1
  • 1
Oo_oO
  • 139
  • 1
  • 13
  • This way you can get storyboard Id NSString *restorationId = self.restorationIdentifier – Rajesh Dharani Mar 11 '17 at 06:07
  • I would have your view controller subclasses implement a class function that indicates whether they should be created from a storyboard or not (actually, I would just use a storyboard for everything) – Paulw11 Mar 11 '17 at 06:12
  • you can Visit http://stackoverflow.com/questions/38590288/how-to-check-if-a-storyboard-exists-in-a-specific-nsbundle – prasad Mar 11 '17 at 07:31
  • visit http://stackoverflow.com/questions/13708660/programmatically-get-a-storyboard-id – prasad Mar 11 '17 at 07:33
  • @prasad `restorationIdentifier` will cause other additional work for app,may have other performance issues. – Oo_oO Mar 11 '17 at 09:21
  • @prasad, neither of those do what the OP is after; the first checks for the existence of a storyboard file (I guess they could a storyboard per view controller and name it appropriately) while the second lets a view controller instance determine if it came from a storyboard. – Paulw11 Mar 11 '17 at 21:43
  • @Paulw11 Cause controller instance not created ,we cannot use controller's storyboard determine if it came from a storyboard.I use your first suggestion ,this sound great ,but if I just add class method though category, this leads to another problem [this link](http://stackoverflow.com/questions/42736049/how-to-override-a-oc-category-method-in-swift/42736858#42736858) (I both use swift and oc) – Oo_oO Mar 13 '17 at 02:48

0 Answers0