通过重写TabbarViewController中的shouldSelect方法来推送View Controller

时间:2019-10-31 12:17:12

标签: ios swift viewcontroller tabbar tabbarcontroller

我想在选择TabBar中的某个项目时推送ViewController,我已编写以下代码,但是它不起作用

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    if let ind = tabBarController.viewControllers!.index(of:viewController) , bottomBarTabsArray[ind].viewType == BottomBarInfo.VIEWTYPE_ASSISTANT { //
          let tabVC = AppStrings.appStoryBoard.instantiateViewController(withIdentifier: "assitantViewControllerID") as! AssitantViewController
        self.selectedViewController?.navigationController?.pushViewController(tabVC, animated: true)
         return false
     }
    return true
}

如何在选择选项卡项而不是切换到ViewController时推动ViewController

1 个答案:

答案 0 :(得分:1)

替换

self.selectedViewController?.navigationController?.pushViewController(tabVC, animated: true)

使用

(self.selectedViewController as! UINavigationController ).pushViewController(tabVC, animated: true)