Unwind not going to the right viewcontroller - ios

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?

You can do this without using unwind segues. Just use popToRootViewController:
navigationController?.popToRootViewController(animated: true)

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

Related

Making my done button reverse segue to the last controller?

I've looked up examples on how to reverse segue in xcode and swift with a storyboard but I can't find an example that pertains to this specific situation where I segue into a navigation controller beforehand. Any pointers?
If you use navigation controller, you can use
self.navigationcontroller?.popviewcontroller(animation: true)
Please try this
It is the same as an ordinary unwind segue.
In your source view controller add this action
#IBAction func unwindToMain(segue: UIStoryboardSegue) {}
And from your destination view (which is titled Action) connect your button to the Exit.
Hope this helps :)

Unwind segue (still) not working

I'm trying to get an unwind segue working (in Swift) and have read through all the "unwind segue not working" posts I could find but haven't yet found whatever it is I'm doing wrong. So.
I have a UISplitViewController with a UITableViewController as my left-hand menu and two UINavigationController+UITableViewController detail views that I'm switching between. Segue-based navigation from my left-hand menu works fine. It's when I want to programmatically unwind that things do not.
I've created an #IBAction function in my first view controller, the one I want to unwind to, like this:
#IBAction func unwindToDocuments(_ segue: UIStoryboardSegue) {
}
(My first view controller is my "Documents" view.)
Then, in the second view controller, I connect an unwind segue to the exit node with identifier unwindSegueToDocuments and action unwindToDocumentsWithSegue:.
It's my understanding that should be all I need to do. However, when I try:
self.performSegue(withIdentifier: "unwindSegueToDocuments", sender: self)
it doesn't do anything. It stays in my second view controller as the detail view.
I'm using self.performSegue there because I'm in a closure. It's some network work so it's being done in a DispatchQueue.async() background queue. However, the performSegue call is in a nested DispatchQueue.main.async() block at the end. (Or, rather, I've tried it both/all ways, and still can't get it to unwind.)
Nothing helpful appears in the console.

Why is my unwind segue crashing my app?

I am trying to create an unwind segue that takes me back two viewControllers, and I thought I had everything set up correctly, but it crashes with no error printed when I perform the segue...
Here's what I've done:
In the DESTINATION viewController, we'll call it VC1 (bottom of the stack) I created this action:
#IBAction func unwind(segue:UIStoryboardSegue){}
In the interface builder I hooked up VC3 (the third view controller up) to the unwind segue via the exit button and named it "unwind".
I call perform for segue like so:
performSegue(withIdentifier: "unwind", sender: self)
Here is the view controllers and segue identifier
http://imgur.com/a/gJPYQ
The "Delete Trip" button calls the segue AFTER all the other logic has been taken care of.
As soon as that is called, my app crashes with no error message and shows me the AppDelegate screen. What's going on?
If you pushed your VC, you could use:
navigationController?.popToRootViewControllerAnimated(true)
If you have two VC's presented modally, then you will need to dismiss two VC's modally. Use:
dismiss(..., completion: {
dismiss(..., completion: nil)
Your current "unwind" segue is simply presenting the original one again..and maybe you don't have identifiers set up correctly or you are overlooking your view hierarchy.

Open the same view from different view controllers

I have the following scenario: I have a view controller and want to navigate programmatically to another view f.e. to give the user the ability to change settings.
So I have VC1 and VC_settings and a present modally segue between those controllers. I gave the segue an identifier and call it like this:
performSegue(withIdentifier: "SegueToSettingsView)
Ok, that works without any problems. The settings view is opened and I can navigate back to VC1.
Now I want to have the same functionality in another view controller VC2. I want to reuse the settings view but the problem is that the segue is already connected between VC1 and VC_settings and I can't connect a new one for VC2.
And if I try to call the existing segue with the coce above in VC2 then the app crashes.
In swift it is possible to create segues from multiple views two one viewcontroller. Here I have set it so there are segues from VC1 to Settings and VC2 to settings which should work.
Hope it helped
Since you have VC1 and VC_settings connected with segue named "SegueToSettingsView" also the perform selector is working fine.
As per your need make another segue from VC2 to VC_Settings and give a different name for it. then make use of below code to perform segue.
performSegue(withIdentifier: "SegueToSettingsViewFromVC2")
Steps to make segue:
Then select the connection and set identifier

Unable to create an unwind segue when using multiple storyboards in a project

I ran into an issue where I had the following setup, but was unable to get an unwind segue to work.
The storyboards were created several months ago, and were refactored from an original main storyboard to split them out into individual ones, with each storyboard having a single view controller.
The storyboards:
MainMenu.storyboard (Contains the navigation controller)
SubMenu.storyboard (Contains the segue that sends the user to the destination storyboard)
Destination.storyboard (Should contain unwind segue)
In the SubMenu viewcontroller, I have the following unwind segue:
#IBAction func unwindToSubMenu(segue: UIStoryboardSegue) {}
I then place a bar button item on the navigation item for the destination view controller. Attempting to link the bar button item to the exit icon on the Destination.storyboard file does not result in the unwindToSubMenu segue appearing.
Also, right clicking on the exit icon results in an empty field.
I did attempt to move the Destination.storyboard contents back to the SubMenu.storyboard to see if it had something to do with the reference links, but this was unsuccessful.
Updated with a better answer.
I ran into this problem again using the latest version of XCode (8.2.1) where a properly configured segue was not appearing.
To make sure I was not insane, I first created a small sample project where I only lifted the relevant code, and was able to confirm that it was set up properly.
With that out of the way, I found a better solution to this issue by creating a garbage storyboard & associated view controller.
The view controller only contained this code:
class GarbageViewController: UIViewController {
#IBAction func unwind(_ segue: UIStoryboardSegue) { }
}
This was then set as the view controller for the garbage storyboard.
I then clicked on the exit outlet in the garbage storyboard file to confirm that the unwind method defined above appeared. Once it appeared, the unwind segue I wanted suddenly became available as well.

Resources