Reputation: 2042
I have a view controller like below.
This view is attached with a tabBarController. The tabBarController has 5 viewControllers and I have to present the 5th viewController of tabBar from another page. So I used the below codes for present that 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)
}
But after execute the code it resulting the view controller as the below image. The view undergoes the tabBar. Anyone help me to push to a tabBar view.
Upvotes: 1
Views: 128
Reputation: 5098
You need to set the selected UIViewController
from UITabBarController
something like this should work .
self.tabBarController?.selectedViewController = self.tabBarController?.viewControllers![1]
where tabBarController?.viewControllers
returns the array of current ViewControllers
embedded in the UITabBarController
.
Your code should be something like this.
@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
}
Note: Don't create an instance of the
UIViewController
as.instantiateViewController(withIdentifier:)
use the already existed ones in the arraytabBarController?.viewControllers
, creating new instance will be treated as new one and gives the problem you have up there .
Upvotes: 1