Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a Table View in a Tab Bar Controller?

I'm starting out with learning Table Views. My applications uses a Tab Bar Controller and all the tabs are simple views. I want to add another tab, and in this tab I'm trying to implement a Table style navigation controller. From what i'm reading these views need a UINavigation Controller as the Root View Controller however my tab bar controller is already set a my Root View Controller, so i'm kinda confused.

Can someone explain what I should be doing to implement something like this.

like image 462
Christian Gossain Avatar asked Feb 12 '11 22:02

Christian Gossain


People also ask

How do I add a tab bar to my view controller?

To add a tab, first drag a new View Controller object to the storybard. Next control-drag from the tab bar controller to new view controller and select view controllers under Relationship Segue . Your tab bar controller will update with a new tab.

What is a tab bar controller?

A Tab Bar Controller is a container view controller that manages an array of view controllers in a radio-style selection interface so that the user can interact with the interface to determine the view controller to display. It is the instance of UITabBarController, which inherits UIViewController.

What is the use of tab bar controller in Xcode?

The tab bar interface displays tabs at the bottom of the window for selecting between the different modes and for displaying the views for that mode. This class is generally used as-is, but may also be subclassed. Each tab of a tab bar controller interface is associated with a custom view controller.

How do I add a tab bar to a storyboard?

Add a new Tab Bar ItemDrag View controller into the storyboard. Drag Tab Bar Item into the newly created view controller, it will sit at the same level of the View .


2 Answers

This is a good question and one that most developers new to Cocoa always struggle with. You need to think about the architecture of your App to see what will make most sense to your requirements, but in most cases you will want the TabBarController to be the main/root controller serving the other views, and then from there configure the tabItem views based on what data you need to display.

For your particular case, when you are creating a new tab item, instead of serving a UIViewController to that tabItem view, you want to use a navigation controller. Then, under the navigation controller, you will be able to assign its root view controller, which should be an instance of UITableViewController (or a UIViewController with a UITableView instance if you need more than a UITableView in your view).

So the hierarchy goes a bit like this

1 -- TabBarController

1.1 -----NavigationController

1.1.1 -------UITableViewController

1.1.1 -------Other views on your nav Controller stack

1.2 -----Any other views on your tabBarController

And here's a good video tutorial that will help you with it: http://www.youtube.com/watch?v=LBnPfAtswgw

Good luck, Rog

like image 76
Rog Avatar answered Sep 27 '22 19:09

Rog


First of all table views do not need necessarily to be embedded in navigation controllers. Most of the time they are, but this is not a requirement.

To add a table view you would create a UITableViewController (or a generic UIViewController with a UITableView embedded in it, this depends on your needs) and put it in your UITabBarController viewControllers property. For example:

UIViewController *vc1 = [[FirstViewController alloc] init];
UIViewController *vc2 = [[SecondsViewController alloc] init];
UITableViewController *tableVC = [[UITableViewController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:vc1, vc2, tableVC, nil];

Of course, above view controllers should be concrete subclasses so you can implement your custom views and logic.

like image 33
tux91 Avatar answered Sep 27 '22 19:09

tux91