A tab bar with multiple tabs using only one view controller

I could not find the answer to my problem on SO, please write to me if the answer already exists somewhere! My problem seems pretty common: I have a table view built into the nav controller and I want the tab bar to contain 3 elements in which 3 elements change the table view to fill in different data, the first tab represents the default data in the table view, when the view loads.

But there is no need to make two additional UITableViewController classes for the other two tabs, when I can just change the data filling the table simple [tableView reloadData]. How do you do this, as well as how to implement this in a storyboard?

+4
source share
4 answers

Sounds to me what you really need is the only UITableViewController with Segmented Control to change the view. Tab tables for navigation .

Segmented control

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

, " ". - - , CoreData fetch/predicate/sort .., Segmented Control - , , . ( , ..), , , UITableViewControllers, UITableViewController, UITableViewController. , .

+2

, viewDidLoad, , tableView, :

- (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;
    }


}
+1

UITabbar . UITabbarViewController, . , UITabbar - .

:

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

. ()

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

segue Ctrl + drag, .

a > 5 ctrl +

CustomTabBarController.swift . TabBarController, .

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 the tag or selected tab index on ViewController.swift

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

let selectedIndex = self.tabController?.selectedIndex
+1
source

All Articles