0

If we have a situation where you have three ViewControllers. Yellow, green and white one.

You can get to the white one through both yellow and green one. See the picture for more clarification:

enter image description here

When I segue from yellow to white, I prepare for segue and set a NSUinteger jimmy_hendrix value to 0;

When I segue from green to white, I prepare for segue and set a NSUinteger jimmy_hendrix value to 1;

Inside the white VC, I made IBAction and connected a BACK button to it. In there, depending on the value I want to unwind the segue which I ended up in the white VC.

This is the code I use:

- (IBAction)goBack:(UIButton *)sender {

switch (_jimmy_hendrix) {

    case 0:
        [self performSegueWithIdentifier:@"fromYellowToWhite" sender:self];
        break;

    case 1:
        [self performSegueWithIdentifier:@"fromGreenToWhite" sender:self];
        break;
}
}

But, I get an exception :

'Receiver (<WhiteVC: 0x15d8c850>) has no segue with identifier 'fromSuperTraenerToSuperTraenerQuestions

To make this clear, I never ctrl + dragged from the BACK button to exit and then selected the unwind segue method, because there are two unwinds I want to perform, based on which VC and which segue I used.

What is a solution to this? How to segue back to whichever ViewvController I used to get to my destionation on the first place?

IMPORTANT: I know I can make new segues, from white to yellow and green and call them. But I don't want that..I don't want to make new segues, I want to programatically unwind the existing ones.

EDIT: I forgot to mention that indeed I made unwind methods in green and yellow VC

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


-(IBAction)fromGreenToWhite:(UIStoryboardSegue *)segue {
}
SteBra
  • 4,188
  • 6
  • 37
  • 68

2 Answers2

2

You need to create the unwind segues. In YellowVC create a method

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

and in GreenVC create a method

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

Then drag from the UIViewController instance in White to the "exit" icon in the White scene and select fromYellowToWhite. Repeat this and select fromGreenToWhite:

Finally, select your newly created unwind Segues and set the identifiers to "fromYellowToWhite" and "fromGreenToWhite" - they should have an 'action' of "fromYellowToWhite:" and "fromGreenToWhite:"

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • He doesn't want to create any new segue. Dismissing the modal view controller (as i suggested) will work. – Berendschot Oct 02 '14 at 10:43
  • 1
    This isn't creating new segues per se - this is the proper way of creating unwind segues and being able to invoke them programatically – Paulw11 Oct 02 '14 at 10:43
  • @Maarten1909 this is not the same as your answer. In your answer you are creating a new instance of the VC each time you go back. An unwind segue will pop back to the existing instance of the VC. – Fogmeister Oct 02 '14 at 10:44
  • @Fogmeister As I'm reading the question, I believe he doesn't want to drag a line from the white vc to the colored ones. So dismissing the modalviewcontroller seems to me the best option. – Berendschot Oct 02 '14 at 10:47
  • Dismissing the view controller isn't the "storyboard way". An unwind segue is. – Paulw11 Oct 02 '14 at 10:48
  • @Maarten1909 yes, that is the confusion here. You can't "unwind an existing segue". What you can do is **create an unwind segue**. (There is a subtle difference). Your original answer will just create a new instance of the Yellow or Green VC. An unwind segue will pop back to the already existing Yellow or Green VC. Yes he might be able to just dismissViewController but then you don't get the functionality of the unwind segue which is why it exists. – Fogmeister Oct 02 '14 at 10:49
  • 1
    True that, I might have explained it poorly. What I ahd in mind is indeed to create an unwind segue. – SteBra Oct 02 '14 at 10:50
  • In that case i suggest reading this post: http://stackoverflow.com/questions/12561735/what-are-unwind-segues-for-and-how-do-you-use-them – Berendschot Oct 02 '14 at 10:51
  • @SteBra no worries. All you are missing is to create the unwind segues from the VC to the exit. (Not from the button to the Exit). Then name the segues and perform them in code. – Fogmeister Oct 02 '14 at 10:52
  • Ok, I did what you guys mentioned, but I still get an error: 'Receiver () has no segue with identifier 'fromMainMenuToSuperTraenerQuestions''. here on this screenshot you can see that I dragged from VC instance to exit for both methods, but I still get an error. Screenshot: https://www.dropbox.com/s/huuy6kbr92aqvtk/Screenshot%202014-10-02%2012.51.37.png?dl=0 – SteBra Oct 02 '14 at 10:54
  • @SteBra did you name the segues? i.e. did you give them an identifier in the inspector? – Fogmeister Oct 02 '14 at 10:55
  • Yeah ofc..here is the screenshot: https://www.dropbox.com/s/45dg7d14ye1omgp/Screenshot%202014-10-02%2012.56.58.png?dl=0 – SteBra Oct 02 '14 at 10:57
  • 1
    @SteBra that's the wrong segue. That is the presenting segue. What you need to give an identifier to is the unwind segue. – Fogmeister Oct 02 '14 at 10:58
  • 2
    That doesn't look right as the screenshot isn't an unwind segue. – Paulw11 Oct 02 '14 at 10:58
  • 1
    Ohhhhhh...I see what I did there. I was calling the SEGUES all the time, not the Unwind segues ! – SteBra Oct 02 '14 at 10:58
1

You don't have to drag from the BACK button to "Exit" but you can drag from the view controller itself to the "Exit". Do this twice. One will be for the Exit to Yellow VC and one will be for the Exit to green VC.

Now in the IB you will have two new segues and you can name them "WhiteToYellowUnwindSegue" etc...

Now to perform these you can...

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

etc...

Fogmeister
  • 76,236
  • 42
  • 207
  • 306