views:

1069

answers:

3

Hi,

I'm currently working on an app that require that I have different UINavigationControllers - So I'm using a tab bar and attempting to use a UITabBar to swap between them so I have in the app delegate a bit of code like so:

// Setting up the views for the tab controller
Friends *friends = [[[Friends alloc] initWithNibName:@"Friends" bundle:[NSBundle mainBundle]] autorelease];
WifiManager *wifi = [[[WifiManager alloc] initWithNibName:@"WifiManager" bundle:[NSBundle mainBundle]] autorelease];

UINavigationController *locationController = [[UINavigationController alloc] initWithRootViewController:wifi];  
UINavigationController *friendsController = [[UINavigationController alloc] initWithRootViewController:friends];

//Set up the tab controller
tabBarController = [[UITabBarController alloc] init];

tabBarController.viewControllers =
[NSArray arrayWithObjects:locationController, friendsController, nil];

//Add the tab bar to the window
[window addSubview:tabBarController.view];

This will compile and will load up the first UINavigationController but when I click on the other navigation controller I get:

*** -[NSCFData tabBarItem]: unrecognized selector sent to instance 0x1152b0'

The strangest part is I can use the tab controller with a single UINavigationController and everything works as it should but when I try and add the second one it fails miserably - has anyone got any ideas as to what I'm doing so wrong here?

Thank you in advance

James

A: 

I have an application that works in a similar way. The way I handle this is to abstract it a little more. That is to say, let the top level (below the default window and stuff) be only the tab-bar controller. I would suggest deriving a custom class here so that you can get at the code. Then have each nav-bar controller reside within that tab-bar (within the nib-structure), but only worry about adding them when they are displayed.

Using Interface Builder: It was pretty simple to do with IB. In my MainWindow.xib file, the top level has all the normal stuff, Window, the generic UITabBarController and the UIViewControllers that I wish to push onto each UINavigationController, we'll say UIViewController 1b and 2b (1a and 2a are the two UIViewControllers that are the default views for each respective navbar). Nested within the UITabBarController, I have the UITabBar and my two UINavigationControllers. Within each, I have a UINavigationBar, a UIViewController, and a UITabBarItem, in that order. Here's what my code looks like in the app delegate:

[window addSubview:tabBarController.view];
[tabBarController setSelectedIndex:0];

Then when I want to make use of the navbars I do this:

UIViewController* newView = [[UIViewController alloc]initWithNibName:@"newViewController" bundle:nil];
[self.navigationController pushViewController:newView animated:TRUE];
[newView release];

That's all it takes for me to get it to work (I might have forgotten some IB wiring).

My final thought is that the bundle might be messing with your navbars. I have never used it and don't know much about the pros and cons, but you might try killing that to see if it's a fix, at least temporarily.

TahoeWolverine
A: 

That code should work.

The only thing I can think to suggest, is to not autorelease the view controllers you are creating.

Kendall Helmstetter Gelner
+3  A: 

Have you verified that each of the single view controllers (Friends and WifiManager) work when there is only one? It could be that your problem is not "two controllers", but "one controller that is broken."

Amagrammer
I just experimented on my own products with Kendall's suggestion about not autoreleasing. I don't think that's your problem, though it's certainly easy to test.
Amagrammer
I was indeed being a bit dim - I had forgotten to to include the - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView method, it seems that with a UINavigationController this is a required method, you'd of thought the compiler would of thrown a warning when it saw it wasn't there...
James Raybould
I once worked with an awesome C programmer who shouted for hours at the compiler for flagging his perfectly ordinary "for loop": if (i = 0; i < 5; ++i) { }We are all dim occasionally.
Amagrammer
P.S. -- on the issue of forgetting the numberOfSectionsInTableView method: If you create your table class using the UITableViewController template, it will be there automatically. They have changed how you select templates, but it's there. (Select NSObject and then, from the select UITableViewController from the "select subclass" control.)
Amagrammer