简体   繁体   中英

Tab bar with multiple tabs using only one view controller

I was not able to find an answer to my problem on SO, please link me if an answer already exists somewhere! My problem seems pretty common- i have a tableview embedded in a nav controller and i want a tab bar with 3 items, where the 3 items change the tableview to populate different data, the first tab representing the default data in the tableview when the view is loaded.

But it seems unnecessary to make two extra UITableViewController classes for the other two tabs, when i can simply just change the data populating the table, with a simple [tableView reloadData] . How do you go about doing this, and also how to implement this in storyboard?

Sounds to me what you really want is a single UITableViewController with a Segmented Control to change the view.Tab Bars are for navigation .

Segmented Control

Use UIControlEventValueChanged to detect the change, load the new data and reload the view.

You didn't indicate what you mean by 'different data'. If you mean something like perform some action which updates data via a CoreData fetch/predicate/sort, etc. then using the Segmented Control is probably the way to go. If your data is dramatically different (different entities, different data management, etc. ) then you should probably go with separate UITableViewControllers as you should not try to over generalize your UITableViewController but rather pair your data to your UITableViewController. After all, that's what it is for.

What you can do is reuse the same Class and have a check in you viewDidLoad method to determine how to populate your tableView , for example:

- (void)viewDidLoad{
    //itemType is a property you set 
    //when instantiating the controller
    int index = self.itemType; 

    switch (index) {
        case 0:
            [self populateTab1];
            break;
        case 1:
            [self populateTab2];
            break;
        case 2:
            [self populateTab3];
            break;
        default:
            break;
    }


}

In storyboard, add UITabbar into your table view controller. Do not use UITabbarViewController which works as a container of view controllers. On the other hand UITabbar is just a control unit.

Set the delegate of the tab bar to your table view controller, and add the following protocol method:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;

You can load the correct data in this method. You can get the index of the selected item (button) by

NSUInteger index = [tabBar.items indexOfObject:item];

You can make multiple relationship segue to one controller with ctrl+drag like this.

storyboard> 5 times ctrl+drag to one Navigation Controller

And you should make CustomTabBarController.swift to modify tabs. Don't forget to change class name of TabBarController that is drawn in storyboard.

class CustomTabBarController: UITabBarController {
    let MENUS = ["tab1", "tab2", "tab3", "tab4", "tab5"]

    override func viewDidLoad() {
        super.viewDidLoad()

        let items = tabBar.items!

        for (var idx=0; idx<items.count; idx++) {
            items[idx].title = MENUS[idx]
            items[idx].tag = idx
        }
    }
...
}

You can use tag or selected index of tabs on the ViewController.swift

let tag = self.tabBarController?.tabBar.selectedItem!.tag

let selectedIndex = self.tabController?.selectedIndex

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM