-segueForUnwindingToViewController: fromViewController: identifier: not being called

你离开我真会死。 提交于 2019-11-30 06:37:42

To add to the answer from @Jeremy, I got unwinding from a modal UIViewController to a UIViewController contained within a UINavigationController to work properly (I.e how I expected it to) using the following within my UINavigationController subclass.

// Pass to the top-most UIViewController on the stack.
- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)
             toViewController fromViewController:(UIViewController *)
             fromViewController identifier:(NSString *)identifier {
   UIViewController *controller = self.topViewController;
   return [controller segueForUnwindingToViewController:toViewController
                                  fromViewController:fromViewController
                                          identifier:identifier];
}

.. and then implementing the segueForUnwindingToViewController as usual in the actual ViewController inside the UINavigationController.

This method should be declared on the parent controller. So if you're using a Navigation Controller with a custom segue, subclass UINavigationController and define this method on it. If you would rather define it on one of the UINavigationController's child views, you can override canPerformUnwindSegueAction:fromViewController:withSender on the UINavigationController to have it search the children for a handler.

If you're using an embedded view (container view), then define it on the parent view controller.

See the last 10 minutes of WWDC 2012 Session 407 - Adopting Storyboards in Your App to understand why this works!

If you're using a UINavigationController and your segue is calling pushViewController then in order to use a custom unwind segue you'll need to subclass UINavigationController and override - (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier.

Say I have a custom unwind segue called CloseDoorSegue. My UINavigationController subclass implementation might look something like:

- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier {

UIStoryboardSegue* theSegue;

if ([identifier isEqualToString:@"CloseDoor"]) {
    theSegue = [CloseBookSegue segueWithIdentifier:identifier source:fromViewController destination:toViewController performHandler:^(void){}];
} else {
    theSegue = [super segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier];
}

return theSegue;

}

Set your UINavigationController subclass as the navigation controller class in the storyboard. You should be good to go provided you have setup the Exit event correctly with "CloseDoor" as the identifier. Also be sure to call 'popViewControllerAnimated' in your unwind segue instead of dismiss to keep in line with UINavigationControllers push/pop mechanism.

iOS development Library There is a discussion on iOS development Library along with this method. - segueForUnwindingToViewController:fromViewController:identifier: Make sure your MyModalViewController is the container role rather than a subcontroller of a container. If there is something like [anotherVC addChildViewController:myModalViewController];,you should put the segueForUnwindingToViewController method in some kind of "AnotherVC.m" file.

Discussion If you implement a custom container view controller that also uses segue unwinding, you must override this method. Your method implementation should instantiate and return a custom segue object that performs whatever animation and other steps that are necessary to unwind the view controllers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!