0

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?

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
steed
  • 61
  • 4
  • Where did you put the `self.performSegue(...)` line of code? That should work fine, assuming you properly created and named the segue... – DonMag Sep 13 '17 at 13:46
  • In tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath). I check the the row corresponds to Disconnect – steed Sep 13 '17 at 14:53
  • OK - so... you added your "unwind" function to `MainVC` class code... you dragged from the little "Class" icon to the "Exit" icon at the top of `HomeTabBarVC` in Interface Builder, and selected your unwind function from the popup... then you selected that segue in Document Outline, and gave it the "unwindToMainVC" identifier? And you have a `print()` or something in your unwind func so you know it's being called? – DonMag Sep 13 '17 at 15:51
  • By the way... do a string search in your project for your unwind function name. Do you happen to have it defined in more than one view controller? – DonMag Sep 13 '17 at 19:18
  • Yes DongMag I did all the steps, I could even see the print.. But now I fixed my problem I'll post the answer – steed Sep 13 '17 at 19:34
  • *sigh* ... I see you are new to Stack Overflow... In the future, please include all relevant information ***including error and/or warning messages*** in your original post. Helps avoid wasting other people's time. – DonMag Sep 13 '17 at 19:48

2 Answers2

1

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

navigationController?.popToRootViewController(animated: true)
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
  • Hello the4kman and thanks for your quick answer ! I am not using a UINavigationController though.. – steed Sep 13 '17 at 12:31
0

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

steed
  • 61
  • 4