Created
August 10, 2013 05:52
-
-
Save mayoff/6199244 to your computer and use it in GitHub Desktop.
This is a generic implementation of prepareForSegue:sender: that calls a segue-specific method based on the segue identifier. There's also a similar implementation of shouldPerformSegueWithIdentifier:sender:.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { | |
NSString *identifier = segue.identifier; | |
if (identifier.length == 0) | |
return; | |
NSString *selectorString = [NSString stringWithFormat:@"prepareFor%@Segue:sender:", identifier]; | |
SEL selector = NSSelectorFromString(selectorString); | |
if (!selector || ![self respondsToSelector:selector]) | |
return; | |
void (*method)(id, SEL, id, id) = (void (*)(id, SEL, id, id))[self methodForSelector:selector]; | |
method(self, selector, segue, sender); | |
} | |
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender { | |
if (identifier.length == 0) | |
return YES; | |
NSString *selectorString = [NSString stringWithFormat:@"shouldPerform%@SegueWithSender:", identifier]; | |
SEL selector = NSSelectorFromString(selectorString); | |
if (!selector || ![self respondsToSelector:selector]) | |
return YES; | |
BOOL (*method)(id, SEL, id) = (BOOL (*)(id, SEL, id))[self methodForSelector:selector]; | |
return method(self, selector, sender); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This implementation of
prepareForSegue:sender:
automatically calls a method specific to the segue. It constructs the segue-specific method name (selector) using the formatprepareFor%@Segue:sender:
, and substitutes the segue identifier for%@
. So, for example, if you create a segue with the identifierDetailPushing
, it will check for a method namedprepareForDetailPushingSegue:sender:
and call the method if it exists.There is also a similar implementation of
shouldPerformSegueWithIdentifier:sender:
. It constructs the segue-specific method name using the formatshouldPerform%@SegueWithSender:
. It only passes the sender, and not the segue identifier, to the segue-specific method. So, for example, if you create a segue with the identifierDetailPushing
, it will check for a method namedshouldPerformDetailPushingSegueWithSender:
and call the method if it exists. It will return whatever the segue-specific method returns. If it can't find a method-specific segue, it just returnsYES
.