2

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?

Scott Berrevoets
  • 16,921
  • 6
  • 59
  • 80
Mr Emkei
  • 43
  • 2
  • 6

3 Answers3

6

[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.

Scott Berrevoets
  • 16,921
  • 6
  • 59
  • 80
  • 2
    +1 Quite right. The only "trick" is how to create an unwind segue in your storyboard (you have to control-drag from the view controller icon in the bar below the scene to the outlet icon), but this technique works great. – Rob Jul 23 '13 at 22:02
  • @Scott - Maybe it should, but I get a new instance of VC A and when I click the done button on that instance, VC A as I left it displays... – Mr Emkei Jul 24 '13 at 14:35
  • @Rob - Im not sure I follow you, I made a manual segue by ctrl-clicking from vc icon in VC D to VC A. What do you mean by "outlet icon" and do you refer to it in VC A or D? – Mr Emkei Jul 24 '13 at 14:37
  • 4
    @MrEmkei I am referring to this technique: http://stackoverflow.com/a/17607083/1271826. The unwind action method is defined in A's view controller and the unwind segue is defined in D's IB scene. If you have a segue you've drawn from D's scene to A's scene in IB, that is not an unwind segue. – Rob Jul 24 '13 at 14:58
  • @Rob - Thanks for taking your time, yes, the way described in the link does work as far as not creating a new instance of VC A. But, the "returned" action first in my question is not called, Ive tested that. I dunno why, it is being called when doing the same from a button to "exit" (which I just tested)...So my main problem remains, who to make call the "returned" action when unwinding programmatically... – Mr Emkei Jul 24 '13 at 20:14
  • @Rob - Thanks, I tried today with a fresh and limited project, but it still does not work, I am sure I making some mistake but cannot find it. I would be happy to share,but do you mean I should post the code here or send it to you, and where? – Mr Emkei Jul 25 '13 at 21:18
  • @Rob - Thanks, I know, I did some programming back in the day (early 90's) and now work with completely different stuff, but I still like programming because its both creative and logic... Ive chosen dropbox, so it seems I need your email to give you access? – Mr Emkei Jul 26 '13 at 18:19
  • @Rob - I am must thankful for you taking your time! I hope I did get this right now and you should be able to access the project through this linK. https://www.dropbox.com/sh/ao5z3brxnqphcg1/Jx3deXEsEd?n=196689440 – Mr Emkei Jul 29 '13 at 10:16
  • @MrEmkei Your `Test12ViewController` has two unwind actions in it, one called `unwindToMainViewController` (which you currently unwind to in the segue with identifier `unwindToMainViewController`, but this method does nothing, so you don't see your alert), and one called `returned` (which you don't use, but would show you your alert if you ever invoked it). Move your `UIAlertView` code from `returned` to `unwindToMainViewController` (and then presumably get rid of `returned`, because you don't use it), and it should work fine. – Rob Jul 29 '13 at 19:25
  • @Rob - Thanks, I know I did two unwind actions but thought I did delete one, I will try this and see how it goes, thanks! – Mr Emkei Jul 30 '13 at 08:29
  • @Rob - Took some time for me to get around to testing your advice, thanks, it works like a charm, I appreciate a lot that you took your time wiht this! – Mr Emkei Aug 10 '13 at 09:34
  • This doesn't work me. Nothing happens. I'm on Xcode 5 and iOS 7. – expert Sep 02 '13 at 07:23
1

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

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.