My question is, how do I get the following custom unwind segue to work on a device with a version prior to iOS 9 as well as on a device running iOS 9?
I
I slightly modified your code.
I don't have to consider whether push or modal.
It seems to work fine.
- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier
{
UIStoryboardSegue *segue;
if ([fromViewController isKindOfClass:[MyViewController class]]) {
segue = [[CustomSegue alloc] initWithIdentifier:identifier source:fromViewController destination:toViewController]; //Custom Unwind Segue
}
else {
if ([super respondsToSelector:@selector(unwindForSegue:towardsViewController:)]) {
[super unwindForSegue:segue towardsViewController:toViewController];
}
segue = [super segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier];
}
return segue;
}
After a lot of trial and error, I found a workaround. I noticed that when you override segueForUnwindingToViewController
, unwindForSegue:towardsViewController
is no longer called, which is the problem. FYI, Apple's note in UIViewController for segueForUnwindingToViewController
says:
Deprecated. Returns a segue that will unwind from the source to destination view controller via the
unwindForSegue:towardViewController:
method. When performing an unwind segue defined in a storyboard, if any view controller along the unwind path has overridden this method and returns non-nil, the runtime will use that segue object instead of constructing an instance of the class specified in Interface Builder.
The portion of the statement in bold does not seem to be implemented, i.e. if you override this method but return nil, unwindForSegue:towardViewController
is still not called and the segue does not occur.
In order to get around this problem, I was able to edit the segueForUnwindingToViewController
in my custom Navigation Controller:
- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier
{
UIStoryboardSegue *segue;
if([fromViewController isKindOfClass:[MyViewController class]]){
segue = [[CustomSegue alloc] initWithIdentifier:identifier source:fromViewController destination:toViewController]; //Custom Unwind Segue
}
else{
if([[UIDevice currentDevice].systemVersion floatValue] < 9.0){
return [super segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier]; //Normal Unwind Segue
}
else{
[super unwindForSegue:segue towardsViewController:toViewController];
return nil;
}
}
return segue;
}
UPDATE: Calling [super segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier];
seems to still work for modal unwind segues. The issue I described seems to happen for show segues. Consequently, if your device runs on a version < 9.0 or if the segue is modal, you should still call the old method. If you call [super unwindForSegue:segue towardsViewController:toViewController];
when the segue is modal, the segue will not work/occur.