Container View Controller no puede manejar la acción Unwind Segue
Frecuentes
Visto 1,773 equipos
2
I am facing a problem while trying to unwind using a custom segue from a view controller that was added as a child to another view controller.
Here is MyCustomSegue.m:
- (void)perform
{
if (_isPresenting)
{
//Present
FirstVC *fromVC = self.sourceViewController;
SecondVC *toVC = self.destinationViewController;
toVC.view.alpha = 0;
[fromVC addChildViewController:toVC];
[fromVC.view addSubview:toVC.view];
[UIView animateWithDuration:1.0 animations:^{
toVC.view.alpha = 1;
//fromVC.view.alpha = 0;
} completion:^(BOOL finished){
[toVC didMoveToParentViewController:fromVC];
}];
}
else
{
//Dismiss
}
}
And here is my FirstVC.m:
- (void)prepareForSegue:(MyCustomSegue *)segue sender:(id)sender
{
segue.isPresenting = YES;
}
- (UIViewController *)viewControllerForUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController withSender:(id)sender
{
return self;
}
- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier
{
return [[MyCustomSegue alloc] initWithIdentifier:identifier source:fromViewController destination:toViewController];
}
- (IBAction)unwindToFirstVC:(UIStoryboardSegue *)segue
{
NSLog(@"I am here");
}
All the necessary connections are done in the storyboard as well.
Mi problema es que -segueForUnwindingToViewController:
is never called.
As soon as -viewControllerForUnwindSegueAction:fromViewController:withSender:
is returned, my program crashes with the following exception:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not find a view controller to execute unwinding for <FirstViewController: 0x8e8d560>'
As of my understanding, the reason for the crash, is that I want my container view controller to be the one to handle the unwind segue action, which is not possible (because container view controller asks only its niños to handle the unwind segue.
Is it correct? What can I do to solve my problem?
¡Gracias!
2 Respuestas
3
I don't think you can use an unwind segue that way (at least I couldn't find a way). Instead, you can create another "normal" segue from the child controller back to the parent, and set its type to custom, with the same class as you used to go from the first controller to the second. In prepareForSegue in the second controller, set isPresenting to NO, and have this code in your custom segue,
- (void)perform {
if (_isPresenting) {
NSLog(@"Presenting");
ViewController *fromVC = self.sourceViewController;
SecondVC *toVC = self.destinationViewController;
toVC.view.alpha = 0;
[fromVC addChildViewController:toVC];
[fromVC.view addSubview:toVC.view];
[UIView animateWithDuration:1.0 animations:^{
toVC.view.alpha = 1;
} completion:^(BOOL finished){
[toVC didMoveToParentViewController:fromVC];
}];
}else{
NSLog(@"dismissing");
SecondVC *fromVC = self.sourceViewController;
[fromVC willMoveToParentViewController:nil];
[UIView animateWithDuration:1.0 animations:^{
fromVC.view.alpha = 0;
} completion:^(BOOL finished) {
[fromVC removeFromParentViewController];
}];
}
}
contestado el 28 de mayo de 14 a las 18:05
-1
If you want to go back from SecondVC to FirstVC, then try using this code where you are telling your controller to go back to previous controller.
[self dismissViewControllerAnimated:YES completion:nil];
This will work and you won't need unwinding code.
Espero que esto ayude.
contestado el 28 de mayo de 14 a las 13:05
Nope, it does not work. I did not present the SecondVC, I added it as a child to the first view controller, so it's my job to dismiss it using my custom segue... - usuario-123
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas ios ios6 segue uicontainerview parentviewcontroller or haz tu propia pregunta.
So basically what you want is to go back to the previous controller, isn't it ? - Dhrumil
Yes, I want to return from secondVC to firstVC - user-123
I've posted an answer below. Do check it and inform me. - Dhrumil