Custom Segue Class for Unwind Segue

2019-04-09 17:07发布

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?

2条回答
干净又极端
2楼-- · 2019-04-09 17:19

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.

查看更多
男人必须洒脱
3楼-- · 2019-04-09 17:36

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.

查看更多
登录 后发表回答