简体   繁体   中英

UITabBarController how to display different UIViewController by pressing tab

I have a UITabBarController with 5 tabs. By pressing on tab I want to make a simple check to determine which UIViewController my UITabBarController should show by pressing a tab.

What is the better way to do it?

Try this one

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    //here it provide the viewController
}

I think that what are you looking for can be achieved by implementing tabBarController(_:didSelect:) (by conforming to UITabBarControllerDelegate ).

It should be similar to (Swift 3):

class ViewController: UIViewController, UITabBarControllerDelegate {
    //...

    override func viewDidLoad() {
        super.viewDidLoad()

        // don't forget to:
        tabBarController?.delegate = self
    }

    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        // here, you can determine what's the selected view controller by checking "viewController":
        if viewController is ViewController {
            // the current selected view controller is "ViewController"
        }
    }

    //...
}

Create a tabbar controller class assign that class to your tabbar and override the method

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
   // your code 
}

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