How to perform Unwind segue programmatically?

IosSegueIos6

Ios Problem Overview


Using storyboard this is very easy. You just drag the action to "Exit". But how should I call it from my code?

Ios Solutions


Solution 1 - Ios

  1. Create a manual segue (ctrl-drag from File’s Owner to Exit),
  2. Choose it in the Left Controller Menu below green EXIT button.

Choose it in the Left Controller Menu below green EXIT button

Insert Name of Segue to unwind.

Then, - (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender. with your segue identify.

Solution 2 - Ios

Here's a complete answer with Objective C and Swift:

  1. Create an IBAction unwind segue in your destination view controller (where you want to segue to). Anywhere in the implementation file.

    // Objective C

     - (IBAction)unwindToContainerVC:(UIStoryboardSegue *)segue {
     
     }
    

    // Swift

    @IBAction func unwindToContainerVC(segue: UIStoryboardSegue) {

     }
    
  2. On the source view controller (the controller you're segueing from), ⌃ + drag from "Name of activity" to exit. You should see the unwind segue created in step 1 in the popup. (If you don't see it, review step one). Pick unwindToContainerVC: from the popup, or whatever you named your method to connect your source controller to the unwind IBAction.

enter image description here

  1. Select the segue in the source view controller's document outline of the storyboard (it will be listed near the bottom), and give it an identifier.

enter image description here

  1. Call the unwind segue using this method from source view controller, substituting your unwind segue name.

// Objective C

[self performSegueWithIdentifier:@"unwindToContainerVC" sender:self];

// Swift

self.performSegueWithIdentifier("unwindToContainerVC", sender: self)

NB. Use the sourceViewController property of the segue parameter on the unwind method to access any exposed properties on the source controller. Also, notice that the framework handles dismissing the source controller. If you'd like to confirm this add a dealloc method to the source controller with a log message that should fire once it has been killed. If dealloc doesn't fire you may have a retain cycle.

Solution 3 - Ios

bradleygriffith's answer was great. I took step 10 and made a screenshot for simplification. This is a screenshot in Xcode 6.

  1. Control-drag from the orange icon to the red Exit icon to create an unwind without any actions/buttons in the view.

enter image description here

  1. Then select the unwind segue in the sidebar:

enter image description here

  1. Set a Segue Identifier string:

enter image description here

  1. Access that identifier from code:
[self performSegueWithIdentifier:@"unwindIdentifier" sender:self];

Solution 4 - Ios

I used [self dismissViewControllerAnimated: YES completion: nil]; which will return you to the calling ViewController.

Solution 5 - Ios

Quoting text from Apple's Technical Note on Unwind Segue: To add an unwind segue that will only be triggered programmatically, control+drag from the scene's view controller icon to its exit icon, then select an unwind action for the new segue from the popup menu.

Link to Technical Note

Solution 6 - Ios

Vishal Chaudhry's answer above worked for me. I would also add that in order to manually trigger the seque using:

[self performSegueWithIdentifier:@"mySegueName" sender:self];

from within the ViewController you must also select the unwind segue under the ViewController's Scene in the storyboard and in the properties view on the RHS ensure that the Indentifier field contains the namer you're referring to in the code ("mySegueName" in the example above).

If you omit this step, the line above will throw an exception that the seque name is not known.

Solution 7 - Ios

Swift 4.2, Xcode 10+

For those wondering how to do this with VCs not set up via the storyboard (those coming to this question from searching "programmatically" + "unwind segue").

Given that you cannot set up an unwind segue programatically, the simplest solely programmatic solution is to call:

navigationController?.popToRootViewController(animated: true)

which will pop all view controllers on the stack back to your root view controller.


To pop just the topmost view controller from the navigation stack, use:

navigationController?.popViewController(animated: true)

Solution 8 - Ios

Backwards compatible solution that will work for versions prior to ios6, for those interested:

- (void)unwindToViewControllerOfClass:(Class)vcClass animated:(BOOL)animated {
    
    for (int i=self.navigationController.viewControllers.count - 1; i >= 0; i--) {
        UIViewController *vc = [self.navigationController.viewControllers objectAtIndex:i];
        if ([vc isKindOfClass:vcClass]) {
            [self.navigationController popToViewController:vc animated:animated];
            return;
        }
    }
}

Solution 9 - Ios

SWIFT 4:

1. Create an @IBAction with segue inside controller you want to unwind to:

    @IBAction func unwindToVC(segue: UIStoryboardSegue) {
    
    }

2. In the storyboard, from the controller you want to segue (unwind) from ctrl+drag from the controller sign to exit sign and choose method you created earlier:

enter image description here

3. Now you can notice that in document outline you have new line with title "Unwind segue....". Now you should click on this line and open attribute inspector to set identifier (in my case unwindSegueIdentifier).

enter image description here

4. You're almost done! Now you need to open view controller you wish to unwind from and create some method that will perform segue. For example you can add button, connect it with code with @IBAction, after that inside this IBAction add perfromSegue(withIdentifier:sender:) method:

     @IBAction func unwindToSomeVCWithSegue(_ sender: UIButton) {
         performSegue(withIdentifier: "unwindSegueIdentifier", sender: nil)
     }

So that is all you have to do!

Solution 10 - Ios

FYI: In order for @Vadim's answer to work with a manual unwind seque action called from within a View Controller you must place the command:

[self performSegueWithIdentifier:(NSString*) identifier sender:(id) sender];

inside of the overriden class method viewDidAppear like so:

-(void) viewDidAppear:(BOOL) animated
{
    [super viewDidAppear: animated];

    [self performSegueWithIdentifier:@"SomeSegueIdentifier" sender:self];
}

If you put it in other ViewController methods like viewDidLoad or viewWillAppear it will be ignored.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontadaszView Question on Stackoverflow
Solution 1 - IosVadimView Answer on Stackoverflow
Solution 2 - IossmileBotView Answer on Stackoverflow
Solution 3 - IosnetwireView Answer on Stackoverflow
Solution 4 - IosLarry GibsonView Answer on Stackoverflow
Solution 5 - IosVishal ChaudhryView Answer on Stackoverflow
Solution 6 - IosJason CrockerView Answer on Stackoverflow
Solution 7 - Ios10623169View Answer on Stackoverflow
Solution 8 - IosJoel TeplyView Answer on Stackoverflow
Solution 9 - Ioswm.p1usView Answer on Stackoverflow
Solution 10 - IosJohn VercoView Answer on Stackoverflow