Reputation: 4858
In my iOS App, number of tab items would not be fixed. It will be decided run time. Suppose, there are 4 tabs I will have to show, then I will have to create 4 instance of the same UIViewController
run time (Inside 4 different navigation controller).
Here is what we do to access a particular view controller:
id controller1 = [self.storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];
[controller1 setTitle:@"Football"];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:controller1];
[arrControllers insertObject:nav1 atIndex:0];
What I want to do is to have 4 instance of navigation controller having instance of a same view controller. All 4 view controllers will have same function to perform (one will show videos of Football, one will show Basketball video..) Means, in story board, MyViewController
is one view controller, but need to create different instances of that MyViewController
How I can do so? The only reason why I will have to do so is, tabs of tabbar will not fixed, those will be dynamic.
Please share anything helpful..
Upvotes: 0
Views: 616
Reputation: 62676
A view controller may be contained by only one other view controller at a time. Even if that weren't the case, I doubt you'd want the very same instance across tabs, since you'd be burdened with changing it's state each time the user changed tab.
The simpler setup is to create unique navigation vcs with unique roots in response to data from the server, e.g....
// you've figured out here that you need four tabs, based on
// a response from the server like...
NSArray *tabNames = @[ @"one", @"two", @"three" @"four"]; // from the server
NSArray *tabs = [@[] mutableCopy];
for (NSString *tabName in tabNames) {
MyViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];
vc.title = tabName;
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[tabs addObject:nav];
}
// here, tabs is an array of view controllers that can
// be assigned to the tabBar viewControllers property
Upvotes: 1