我有一个如下的视图控制器。
该视图附带一个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视图。
最佳答案
您需要设置从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
中的那些,创建新实例将被视为新实例,并给出您遇到的问题
那里 。