UITabBarController如何通过按Tab键显示不同的UIViewController

时间:2017-04-24 11:57:00

标签: ios swift uitabbarcontroller

我有一个带有5个标签的UITabBarController。通过按Tab键我想做一个简单的检查,以确定我的UITabBarController应该通过按Tab键显示哪个UIViewController。

最好的办法是什么?

3 个答案:

答案 0 :(得分:1)

试试这个

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

答案 1 :(得分:1)

我认为您可以通过实施tabBarController(_:didSelect:)(符合UITabBarControllerDelegate)来实现您的目标。

它应该类似于(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"
        }
    }

    //...
}

答案 2 :(得分:1)

创建一个tabbar控制器类,将该类分配给tabbar并覆盖方法

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