问题
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:
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 {
}
回答1:
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:"
回答2:
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...
来源:https://stackoverflow.com/questions/26159014/how-to-unwind-programmatically-to-2-different-vc