Good approach for multiple view controllers for navigation projects

So, I want the RootViewController to be able to handle a different view controller for each cell (okay, I have reasons not to repeat using nibs here).

I can list them all in the didSelectRowAtIndexPath file as follows:

if(condition){ DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]]; dvController.selectedCountry = selectedCountry; [self.navigationController pushViewController:dvController animated:YES]; [dvController release]; } else if (condition2){ DetailViewController2 *dvController2 = [[DetailViewController2 alloc] initWithNibName:@"DetailViewController2" bundle:[NSBundle mainBundle]]; dvController2.selectedCountry = selectedCountry; [self.navigationController pushViewController:dvController2 animated:YES]; [dvController2 release]; dvController2 = nil; } 

but it can be quite a long time, and I don’t know another way to do it. Are there any "special controllers" or classes that I can use to solve this problem? I am new to iOS development, so I know a little.

Thanks in advance!

+4
source share
1 answer

I think you can use the Factory method that returns a UIViewController object, as this is the type of the pushViewController argument (as you can see in the API docs for the UINavigationController ). Basically, you create a method that expects int, String, Enum to tell you which kind of DetailView you want to create, then it creates an object of this implementation of DetailView and returns it. Then you can click this new controller using pushViewController .

+4
source

All Articles