Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform Unwind segue programmatically?

Tags:

ios

ios6

segue

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

like image 925
tadasz Avatar asked Sep 20 '12 09:09

tadasz


2 Answers

  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.

like image 139
Vadim Avatar answered Oct 04 '22 00:10

Vadim


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

3) 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

4) 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.

like image 36
smileBot Avatar answered Oct 04 '22 00:10

smileBot