2

I have a tabBarController with 2 tabs:

tab0, tab1

In tab0 I have a navigationController with 3 child views

viewA (root), viewB, viewC

pressing a button in viewC will bring me to tab1 using code

@IBAction func switchButtonTapped(sender: UIButton){
    tabBarController.selectedIndex = 1
}

The problem I'm having is that once I switch to tab1 I can't get tab0 to reset back to viewA (its root vc), it stays on viewC.

How do I switch from tab0 to tab1 and at the same time reset the views in tab0?

Since I'm simultaneously switching tabs and resetting a nav controller's vcs should this happen on different threads?

 @IBAction func switchButtonTapped(sender: UIButton){
        tabBarController.selectedIndex = 1
        dispatch_async(dispatch_get_main_queue(), {
self.navigationController?.popToRootViewController(animated:true)
} 
    }
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256
  • 1
    No, it all needs to happen on the main thread as you are doing now. In general what you're trying to do should work. Have you tried changing the `animated` parameter to `false`? You don't really want an animation here. – Dima Oct 16 '16 at 06:03
  • @Dima it works both ways. Thanks for the help – Lance Samaria Oct 17 '16 at 04:56

1 Answers1

4

Note: Swift 3 Code:

@IBAction func switchButtonTapped(sender: UIButton){
    tabBarController?.selectedIndex = 1
    navigationController?.popToRootViewController(animated: true)
}

This works fine for me (it selects the second tab and when I tap on the first tab button, It shows the root -first- ViewController for the first tab).

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • 1
    I show an implementation of this from a different tab as my answer to this question (http://stackoverflow.com/questions/32716918/popping-a-view-controller-in-different-tab/40857974#40857974) for those looking for that solution. I use NSNotifications to do the trick so that you can achieve this from anywhere in code. – Joseph Astrahan Nov 29 '16 at 04:46