0

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?!

7
  • try self.navigationController?.tabBarController?.selectedIndex instead? Commented Jun 13, 2018 at 8:52
  • @koropok thank you for your answer,I had to set pop to root view after your suggestion and it's working perfectly! thank you!! Commented Jun 13, 2018 at 8:55
  • @koropok I would suggest to add it as an answer so the OP can accept it. Commented Jun 13, 2018 at 9:00
  • @AhmadF alright, thanks. Commented Jun 13, 2018 at 9:03
  • @AzinNilchi could you please accept the answer? FYI, accepting an answer leads to: - gives the answerer +15 reputation points. - gives you (asker) +2 reputation points. - And the most important thing, it gives an indication for the viewers that this answer is the appropriate solution for this issue. Commented Jun 13, 2018 at 9:15

2 Answers 2

3

You have to use this instead.

 self.navigationController?.tabBarController?.selectedIndex 
Sign up to request clarification or add additional context in comments.

1 Comment

It it still working in Swift 5 ? I have tried but was not able to change the selected tab
0

you can change selectedIndex and then call popToRootViewController. this do for me.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.