我有一个如下的视图控制器。

ios - 在按下TabBar时隐藏 View - swift-LMLPHP

该视图附带一个tabBarController。 tabBarController有5个viewController,我必须从另一页上介绍tabBar的第5个viewController。所以我用下面的代码来展示viewController

@IBAction func onClickUserProfile(_ sender: Any) {
        let navVc = self.storyboard?.instantiateViewController(withIdentifier: "ProfileVC")as! ProfileVC
        navVc.userId = Int(self.userId)
        navVc.navigationItem.hidesBackButton = true
        navVc.tabBarController?.tabBar.isHidden = false
        self.navigationController?.pushViewController(nxtVc, animated: true)
    }

但是在执行代码后,它导致视图控制器如下图所示。
视图将经过tabBar。任何人都可以帮助我推送到tabBar视图。

ios - 在按下TabBar时隐藏 View - swift-LMLPHP

最佳答案

您需要设置从UIViewController中选择的UITabBarController,这样应该可以。

self.tabBarController?.selectedViewController = self.tabBarController?.viewControllers![1]

其中tabBarController?.viewControllers返回嵌入在ViewControllers中的当前UITabBarController的数组。

您的代码应该是这样的。
@IBAction func onClickUserProfile(_ sender: Any) {
        let vc =  self.tabBarController?.viewControllers![1] as! ProfileVC // use your index
        vc.userId = Int(self.userId)
        self.tabBarController?.selectedViewController = vc
    }

注意:不要使用已存在的UIViewController创建.instantiateViewController(withIdentifier:)的实例
数组tabBarController?.viewControllers中的那些,创建新
实例将被视为新实例,并给出您遇到的问题
那里 。

10-08 07:27
查看更多