防止tabBar移动到下一个View Controller onselect

时间:2018-07-17 17:38:44

标签: ios swift

我的tabBar上有一个项目,我实际上不想移动到它的视图控制器,但是当单击该项目时,会发生一些事情(当前视图控制器上会出现一个弹出对话框)。

我当前拥有当前代码:

class TabViewController: UITabBarController, UITabBarControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // tell our UITabBarController subclass to handle its own delegate methods
        self.delegate = self

    }

    // called whenever a tab button is tapped
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {

        if viewController is PostTabViewController {
            ... code here ...

        } 
    }


}

此处..code处的代码运行正常,但是仍显示PostTabViewController。我将如何停止它?

1 个答案:

答案 0 :(得分:0)

您应该在tabBarController(_, shouldSelect:)

中进行检查
func tabBarController(UITabBarController, shouldSelect: UIViewController) -> Bool {
    guard viewController is PostTabViewController else {
        return true
    }

    ... code here ...
    return false
}