Adding NavigationControl to a TabBar Application containing UITableViews

Posted by kungfuslippers on Stack Overflow See other posts from Stack Overflow or by kungfuslippers
Published on 2009-08-13T08:46:01Z Indexed on 2010/03/25 5:03 UTC
Read the original article Hit count: 311

Hi,

I'm new to iPhone dev and wanted to get advice on the general design pattern / guide for putting a certain kind of app together.

I'm trying to build a TabBar type application. One of the tabs needs to display a TableView and selecting a cell from within the table view will do something else - maybe show another table view or a web page. I need a Navigation Bar to be able to take me back from the table view/web page.

The approach I've taken so far is to:

Create an app based around UITabBarController as the rootcontroller

i.e.

@interface MyAppDelegate : NSObject <UIApplicationDelegate> 
{
IBOutlet UIWindow *window;
IBOutlet UITabBarController *rootController;
}

Create a load of UIViewController derived classes and associated NIBs and wire everything up in IB so when I run the app I get the basic tabs working.

I then take the UIViewController derived class and modify it to the following:

@interface MyViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> 
{

}

and I add the delegate methods to the implementation of MyViewController

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section         { 
return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
	cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

if (indexPath.row == 0)
{
	cell.textLabel.text = @"Mummy";		
}
else
{
	cell.textLabel.text = @"Daddy";
}
return cell;
}

Go back to IB , open MyViewController.xib and drop a UITableView onto it. Set the Files Owner to be MyViewController and then set the delegate and datasource of the UITableView to be MyViewController.

If I run the app now, I get the table view appearing with mummy and daddy working nicely. So far so good.

The question is how do I go about incorporating a Navigation Bar into my current code for when I implement:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath()
{
// 	get row selected
NSUInteger row = [indexPath row];

if (row == 0)
{
	//	Show another table
}
else if (row == 1)
{
	//	Show a web view
}
}

Do I drop a NavigationBar UI control onto MyControllerView.xib ? Should I create it programmatically? Should I be using a UINavigationController somewhere ? I've tried dropping a NavigationBar onto my MyControllerView.xib in IB but its not shown when I run the app, only the TableView is displayed.

© Stack Overflow or respective owner

Related posts about uitableview

Related posts about uinavigationbar