单击选项卡栏图标时重置ViewController

时间:2019-03-30 18:20:14

标签: swift xcode viewcontroller tabbar

我的标签栏中有3个项目,每个项目都链接到一个单独的viewController,并且我希望每次在这些项目之间进行切换时都将它们重置。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以为标签栏控制器创建一个子类,并在其中执行所需的操作。

class TabBarViewController: UITabBarController, UITabBarControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.delegate = self
    }

    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {

         let viewController0: UIViewController? = tabBarController.viewControllers?[0] as? UIViewController
         let viewController1: UIViewController? = tabBarController.viewControllers?[1] as? UIViewController
         let viewController2: UIViewController? = tabBarController.viewControllers?[2] as? UIViewController

         switch self.selectedIndex {
         case 0:
             // Refresh viewController1
             // Refresh viewController2
         case 1:
             // Refresh viewController2
             // Refresh viewController3
         case 2:
             // Refresh viewController0
             // Refresh viewController1
         default: 
             break
         }
    }

}