iosswiftuinavigationcontrolleruitabbarcontroller

how to change tab immediately after pop to rootView Swift?


I have a tab view with 5 tabs, and each tab are embeded in navigation controller! in tab 1, I enter view controller A, and then entered to viewController B! on a button in B view Controller, I want to pop to root view, and change the tab! and I used this code :

  self.navigationController?.popToRootViewController(animated: true)                  
  self.tabBarController?.selectedIndex = 4

but it only pop to root view, and does not changing the tab! how can I do both of them?!


Solution

  • You have to use this instead.

     self.navigationController?.tabBarController?.selectedIndex