2
votes

Context: I have two VC, A and B. VC A contains several buttons and several labels. When pressing the button in VC A a segue will display VC B/C/ and so forth. Now, when finished with VC A/B/C so forth, the segue is being unwind so that VC A appears. For most of the VC B/C/D so forth, I am using the unwind method which I trigger through a button in that VC (ctrl + drag to "exit" icon). This works perfect, because upon returning to VC A, the following action is being called automatically:

- (IBAction)returned:(UIStoryboardSegue *)segue {
// Here I do some stuff 
}

Problem: Now, in one of the secondary VCs (e.g. D), things are a bit special. In this VC I generate some hundred buttons through a loop programmatically, then detect which button is being pressed and finally unwind back to VC A (without a specific button; any of the buttons will trigger the unwind). I know I can do this eg by using this

[self dismissViewControllerAnimated: YES completion: nil]

but this does not trigger the above action when returning to VC A, or by using this

[self performSegueWithIdentifier:@"UnwindSegueIdentifier" sender:self]

but this will generate a new instance of VC A, which I do not want (because labels in the instance of VC A already contains some information).

So, what I want is to be able to return to the same instance of VC A which generated the VC D, and also trigger the "returned" action listed above. Thus, I want to achieve the same effect as when using a button connected to the "exit" icon, but I want to do this programmatically "inside the code" when one the many buttons in VC D is pressed.

Any thoughts?

3

3 Answers

6
votes

[self performSegueWithIdentifier:@"UnwindSegueIdentifier" sender:self] should work just fine. Unwind segues do not create new instances of their destinations, no matter how they're performed. They are unique in that it's the only segue that does not create a new instance of the destination.

1
votes

You can create a unwindsegue by to use programmatically by control+dragging from the scene's view controller icon to its exit icon. After assigning an identifier to this unwindseque, you can call it programmatically.

Reference: https://developer.apple.com/library/ios/technotes/tn2298/_index.html

0
votes

Ulas has properly understood and answered the question. The original poster wanted to ensure that the unwind '(IBAction)myCustomName:(UIStoryboardSegue*)segue' selector was called upon unwind without having to 'manually' link a button to the unwind segue. To emphasize this point, assume that you programmatically create a button at runtime based on some condition. You do not have the ability to manually wire that button to the unwind segue. You need to do something like this within VC B...

UIBarButtonItem *dynamicButton = [[UIBarButtonItem alloc]
                       initWithTitle:@"Cool"
                       style:UIBarButtonItemStyleBordered
                       target:self
                       action:@selector(killView)];
self.navigationItem.leftBarButtonItem = dynamicButton;


- (void) killView
{
    [self performSegueWithIdentifier:@"TEST" sender:self];
} 

The 'TEST' segue was established from the VC B to Exit manually (i.e the view controller itself not a button). When the button is clicked, it calls the killView method and the segue is triggered via the identifier.