3

I have a Navigation-Based Application that shows a TableView where you can select a cell and it brings you to a "Detail View" for that cell. I want this view to then have a TabBar where I can select between 3 subviews. I have found several solutions online for this but none are very helpful. Is there a tutorial for this specifically or is their source code indicating how it can be done? Thanks

3 Answers 3

6

Basically What you need to do is push a Tab View Controller onto the Navigation Controller's viewcontroller stack.

Starting with a fresh "Navigation-Based Application" template. I added the following method in RootViewController.m :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Navigation logic may go here. Create and push another view controller.
UIViewController *viewOneViewController =   [[UIViewController alloc] init];
viewOneViewController.title = @"One";
viewOneViewController.view.backgroundColor = [UIColor redColor];

UIViewController *viewTwoViewController =   [[UIViewController alloc] init];
viewTwoViewController.title = @"Two";
viewTwoViewController.view.backgroundColor = [UIColor orangeColor];

UIViewController *viewThreeViewController = [[UIViewController alloc] init];
viewThreeViewController.title = @"Three";
viewThreeViewController.view.backgroundColor = [UIColor greenColor];

UITabBarController *anotherViewController = [[UITabBarController alloc] init];
anotherViewController.viewControllers = [NSArray arrayWithObjects:viewOneViewController, viewTwoViewController, viewThreeViewController, nil];
[self.navigationController pushViewController:anotherViewController animated:YES];
[anotherViewController release];

}

Changed this to 25 to test:

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

Now when I build and run I'll see what you are looking for in a basic way. What you will want to do after you get this working is to change the UIViewControllers to Custom Subclasses that you create to hold the code for each view. (If you are also using Interface Builder, change the init to initWithNibNamed:).

Hope this helps you get on your way a bit.

2
  • You should also want to create a custom subclass of UITabBarController, in which you allocate its viewControllers, thats more clean then put everything in the didSelectRow method. Next to that, you forgot some 'release's. But still + Feb 22, 2011 at 21:35
  • if we wan't tabs with xib then..?? May 8, 2013 at 11:33
2

You should be aware that it's possible Apple will reject your application if you do this though.

From "Combining Tab Bar and Navigation Controllers"

It’s very common to combine tab bar and navigation controllers, as illustrated in Figure 4. To do this, you simply add navigation controllers to a tab bar controller (however, you should never add a tab bar controller to a navigation controller).

So it's likely to be against the Human Interface guidelines and so could you get rejected.

1

I'm trying to do this very same thing. The application "Tweetie" is doing something similar. They have a TableView of accounts and then you select an account and TabBar appears.

Anyways, straight from Apple's Documentation:

pushViewController:animated: Pushes a view controller onto the receiver’s stack and updates the display.

  • (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated

Parameters viewController The view controller that is pushed onto the stack. It cannot be an instance of tab bar controller. This method does nothing if the view controller is already on the stack.

animated Set this value to YES to animate the transition. Pass NO if you are setting up a navigation controller before its view is displayed.

So maybe we're just stuck using a TabBar and not a TabBarController?

-JP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.