Unwind not going to the right viewcontroller

175 Views Asked by At

I am trying to use an unwind segue to logout from my app.

Here is my view hierarchy:

MainVC->(show)->ConnectionVC->(show)->HomeTabBarVC->SettingsVC (4th tab of the HomeTabBarVC containing a tableview)

MainVC, ConnectionVC and HomeTabBarVC are in three different Storyboards.

When pressing the "Disconnect" row of SettingsVC, I want to unwind to MainVC.

I already tried to follow this:

What are unwind segues and how do you use them?

and

Unwind segue and nav button items not triggering after tab bar controller added

The unwindToThisViewController function (which I put in MainVC) from the tutorial is triggered but the only thing that happens is going back to the first tab of HomeTabBarVC.

I tried to execute this action in SettingsVC in two ways:

(self.tabBarController! as! HomeTabBarVC).performSegue(withIdentifier: "unwindToMainVC", sender: self)

and

self.performSegue(withIdentifier: "unwindToMainVC", sender: self)

Neither are working...

Any toughts?

2

There are 2 best solutions below

1
On

You can do this without using unwind segues. Just use popToRootViewController:

navigationController?.popToRootViewController(animated: true)
0
On

I found a solution : after seeing that I had the "Presenting view controllers on detached view controllers is discouraged " warning, I created a Delegate to execute the segue in my HomeTabBarVC and it works fine! Thanks everyone for your help!!