Custom Segue Class for Unwind Segue

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-01 13:57:36

问题


A Standard / Forward Segue can be clicked on in the Storyboard builder and the style can be changed to Custom. At this point a Segue Class can be specified. However, this is not possible for an Unwind Segue. Unwind Segues only have Identifier and Action to specify. It reasons that their type is "Unwind Segue" but is there a way to create a custom Segue class for these Unwind Segues?


回答1:


I found that in order to implement a custom unwind segue I had to subclass the UINavigationController and override this method as follows:

-(UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier {
UIViewController *controller = self.topViewController;
UIStoryboardSegue *unwindSegue;

if ([controller isKindOfClass:[YourClassThatNeedsCustomUnwind class]]) {
    unwindSegue = [controller segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier];
}

if (unwindSegue) {
    return unwindSegue;
} else {
    return [super segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier];
}
}

Then in the UIViewController where the custom unwind segue is needed override this method:

 - (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier {
YourCustomUnwindSegue *segue = [[YourCustomUnwindSegue alloc] initWithIdentifier:identifier source:fromViewController destination:toViewController];
return segue;
 }

Lastly in your custom UIStoryboardSegue class just override the "-(void)perform:" method and put whatever animations (or in my case lack of animations) you need.

- (void)perform
{
UINavigationController *containerVC = (UINavigationController*)[self.destinationViewController parentViewController];
[containerVC popToViewController:self.destinationViewController animated:NO];
}

Hope this helps someone out.




回答2:


Maybe you want to have a look at http://dadabeatnik.wordpress.com/2013/10/13/custom-segues/ article. It explains custom segues in great detail.



来源:https://stackoverflow.com/questions/20384946/custom-segue-class-for-unwind-segue

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