gangelo
gangelo

Reputation: 3182

How to unwind to a ViewController in another storyboard programmatically?

This has been driving me nuts for almost 2 days now. I've tried just about everything, and can't get any results. Every post that addresses unwinding, doesn't seem to specifically address unwinding back to a ViewController in a different storyboard, let alone doing it programmatically. What am I doing wrong?????

I have two ViewControllers that reside in different storyboards...

I want to unwind from my SignupViewController located in my Signup storyboard to my LoginViewController located in my Login storyboard. I want to perform the unwind programmatically after the user successfully registers, receives a confirmation alert with an OK button, and clicks the OK button.

This is where I want to unwind from => to

enter image description here

This is how I am attempting to do it

So I create an unwindToLoginViewController action method in my LoginViewController.swift file like so...

enter image description here

And create a reference to it in my SignupViewController.swift file like so...

enter image description here

Subsequently, I get the reference as expected...

enter image description here

And set the Identifier in the same, so I can reference it in my code...

enter image description here

When I run my app, successfully register a user, I display a confirmation alert with an OK button. When the user clicks the OK button, I want to unwind to my LoginViewController, but NOTHING happens...

Any clues?????????

enter image description here

Upvotes: 3

Views: 1091

Answers (1)

Rob
Rob

Reputation: 437882

Unwind segues should only be used to unwind back to a view controller that already has been presented and is still in the view controller hierarchy.

In comments, above, it's clear that this is not the case. It would appear that your intent is to dismiss everything currently in the view hierarchy and replace it with a completely different scene (a login scene in your example). In that case, you should just replace the root view controller with something like (as shown in https://stackoverflow.com/a/41144822/1271826):

let window = UIApplication.shared.keyWindow!
let frame = window.rootViewController!.view.frame

let controller = UIStoryboard(name: "Login", bundle: nil)
    .instantiateViewController(withIdentifier: "...")!

controller.view.frame = frame

UIView.transition(with: window, duration: 0.3, options: .transitionCrossDissolve, animations: {
    window.rootViewController = controller
}, completion: { completed in
    // maybe do something here
})

But let's assume for a second that you did want to use an unwind segue because you thought the view controller would be in the view hierarchy. (I know that's not the case here, but for the sake of future readers, I'll going to explore this scenario.)

Bottom line, if an unwind segue ever fails, you should first confirm that the view controller with the unwind IBAction actually is in the view controller hierarchy.

If you're ever unsure about the view controller hierarchy, pause execution of your app and print the hierarchy:

(lldb) expr -l objc++ -O -- [UIViewController _printHierarchy] 
<UINavigationController 0x7ff341047800>, state: appeared, view: <UILayoutContainerView 0x7ff340f15610>
   | <MyApp.FirstViewController 0x7ff340c1d2f0>, state: disappeared, view: <UIView 0x7ff340c336a0> not in the window
   | <MyApp.SecondViewController 0x7ff340d08880>, state: disappeared, view: <UIView 0x7ff340d09620> not in the window
   | <MyApp.ThirdViewController 0x7ff3433089d0>, state: disappeared, view: <UIView 0x7ff343104b80> not in the window
   | <MyApp.FourthViewController 0x7ff343102810>, state: disappeared, view: <UIView 0x7ff340d0b150> not in the window
   | <MyApp.FifthViewController 0x7ff340f19c60>, state: appeared, view: <UIView 0x7ff340d02010>

Just make sure that the view controller with the unwind action is shown in the hierarchy.

Bottom line, you can only unwind to something currently in the view controller hierarchy, so make sure it's there.

Upvotes: 5

Related Questions