Tab Bar and Nav Controller: Where did I go wrong in my Interface Builder wiring?
- by editor
Even if you don't know how I've shot
myself in the foot, a story which I've tried
to lay out below, if you think I've
done a good job showing the parameters
of my problem I'd appreciate an upvote so
that I might be able to grab some
attention for my question.
I've been working on an iPhone application in XCode and Interface Builder of the Tab Bar project type. After getting a table view of topics (business sectors) working fine I realized that I would need to add a Navigation Control to allow the user to drill into a subtopics (subsectors) table.
As a green Objective-C developer, that was confusing, but I managed to get it working by reading various documentation trying out a few different IB options. My current setup is a Tab Bar Controller with Tab 1 as a Navigation Controller and Tab 2 a plain view with a Table View placed into it. The wiring works: I can log when table rows are selected and I'm ready to push a new View Controller onto the stack so that I can display the subtopics Table View.
My problem: For some reason the first tab's Table View is a delegate and dataSource of the second ta. It doesn't make sense to me and I can't figure out why that's the only setup that works.
Here is the wiring:
Navigation Controller (Sectors) is a
delegate of Tab Bar
Navigation Bar is
a delegate of Navigation Controller
(Sectors)
View Contoller (Sectors) has a view of Table View
Table View (in Navigation
Controller (Sectors)) is a delegate
of First View Controller
(Companies)
Table View (in Navigation
Controller (Sectors)) is a dataSource outlet of First View Controller
(Companies)
First View Controller (Companies)
First View Contoller (Sectors) has a view of Table View
Table View (in First View Controller (Companies)) is not hooked up to a dataSource outlet and is not a delegate
When I click the tab buttons and look at the Inspector I see that the first tab is correctly hooked up to my MainWindow.xib and the second tab has selected a nib called SecondView.xib. It's in the File's Owner of MainWindow.xib where I inherit UITableViewDataSource and UITableViewDelegate (and also UITabBarControllerDelegate) in the .h, and in the .m where I implement the delegate methods.
Why does this setup only work when the Table View in my first tab (View Controller (Sectors)) is a delegate and dataSource of the second tab? I'm confused: why wouldn't it need to be hooked up to the Navigation Controller-enabled tab in which the Table View is seen (Navigation Controller (Sectors))? The Table View seen on the second tab has neither dataSource and is not a delegate.
I'm having trouble getting a pushViewController to fire (self.navigationController is not nil but the new View Controller still doesn't load) and I suspect that I need to work out this IB wiring issue before I can troubleshoot why the Nav Controller won't push a new View Controller onto the stack.
if(nil == self.navigationController) {
NSLog(@"self.navigationController is nil.");
} else {
NSLog(@"self.navigationController is not nil.");
SectorList *subsectorViewController = [[SectorList alloc] initWithNibName:@"SectorList" bundle:nil];
subsectorViewController.title = @"Subsectors";
[[self navigationController] pushViewController:subsectorViewController animated:YES];
[subsectorViewController release];
}