Reputation: 718
I tried many options, but none of them work. The problem is simple :
I have got one UITabBarController, which works perfectly for 5 tabs.
My problem is located on first and second tabs : I need to use the same view controller under each tab, but of course with 2 distinct instances.
I tried to add 2 distinct navigation controllers, all pointing to the same view controller as root controller. This way I can customize the tab text and icon (I work mainly in interface builder for this) on each navigation controller.
__ NavController 1 _
/ \
TabController -- NavController 2 -- ViewController
With this configuration : only the first tab works (the one handled by NavController1), the second does not show the view (I absolutely don't know which view is shown, but it's empty, black and has a default empty blue nav bar). If I keep only one link from navigation controller to my view controller, like this :
__ NavController 1
/
TabController -- NavController 2 -- ViewController
The second tab works perfectly !
The other option is to link the NavController twice, but in this case I need to configure title and image programmatically depending on the tab index and I am not sure where would be the place to do this.
So :
Upvotes: 4
Views: 2631
Reputation: 104082
You should be able to do this in IB. Just add two navigation controllers as the base controllers of two of the tabs, add root view controllers to each of them (not just one like you showed in your question), and change the class of those root view controllers to your subclass.
After Edit
If you have a complicated scene that you made as one of the root view controllers, you can copy and paste it, and another one will appear in the objects list on the left side of the IB window (and on the canvas as well, but it's right on top of the other one, so you need to drag it off to see it). You can connect a second navigation controller to it. You can either do it that way, or drag in two different UIViewControllers and change them both to your subclass (if you need different looking interfaces for the 2 different instances).
Upvotes: 1
Reputation: 1735
You can use code similar to this in your app delegate, if you want to do it programatically.
UITabBarController *tbc = [[UITabBarController alloc] init];
MyViewController *mvc1 = [[MyViewController alloc] init];
MyViewController *mvc2 = [[MyViewController alloc] init];
mvc1.title=@"One";
mvc2.title=@"Two";
mvc1.tabBarItem.image=[UIImage imageNamed:@"one.png"];
mvc2.tabBarItem.image=[UIImage imageNamed:@"two.png"];
UINavigationController *nc1 = [[UINavigationController alloc] initWithRootViewController:mvc1];
UINavigationController *nc2 = [[UINavigationController alloc] initWithRootViewController:mvc2];
[tbc setViewControllers:[NSArray arrayWithObjects:nc1,nc2, nil]];
self.window.rootViewController = tbc;
Upvotes: 1