Created
July 26, 2013 17:16
-
-
Save boredzo/6090585 to your computer and use it in GitHub Desktop.
Generic NSMenuItem/NSControl validation method, so you can implement a separate -validate<Action>: method for each action
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
//This validation method is completely generic; it dispatches to validate<Action>:, where <Action> is the item's action, duly initially-capitalized. | |
- (BOOL) validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)item { | |
SEL action = [item action]; | |
if (![self respondsToSelector:action]) { | |
//We don't respond to the action, so unconditionally return NO. | |
return NO; | |
} | |
/*Determine the selector of the message to send ourselves for actual validation. | |
*/ | |
NSString *actionString = NSStringFromSelector(action); | |
NSString *firstCharacter = [actionString substringToIndex:1]; | |
NSString *andTheRest = [actionString substringFromIndex:1]; | |
NSString *validationSelectorString = [NSString stringWithFormat:@"validate%@%@", | |
[firstCharacter uppercaseString], | |
andTheRest | |
]; | |
if (![validationSelectorString hasSuffix:@":"]) | |
validationSelectorString = [validationSelectorString stringByAppendingString:@":"]; | |
SEL validationSelector = NSSelectorFromString(validationSelectorString); | |
if ([self respondsToSelector:validationSelector]) | |
return (BOOL)objc_msgSend(self, validationSelector, item); | |
//Well, we do respond to the action, so default to YES. | |
return YES; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment