Last active
December 15, 2015 07:10
-
-
Save berikv/5221867 to your computer and use it in GitHub Desktop.
UIResponder sendAction
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
// | |
// UIResponder+sendAction.h | |
// Berik Visschers | |
// | |
// Created by Berik Visschers on 2013-03-22. | |
// Copyright (c) 2013 Xaton. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
#define propagateAction() ([self sendAction:_cmd]) | |
@interface UIResponder (sendAction) | |
- (BOOL)sendAction:(SEL)action; | |
- (BOOL)sendAction:(SEL)action forEvent:(UIEvent *)event; | |
- (BOOL)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event; | |
- (BOOL)sendAction:(SEL)action to:(id)target from:(id)sender forEvent:(UIEvent *)event; | |
@end |
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
// | |
// UIResponder+sendAction.m | |
// Berik Visschers | |
// | |
// Created by Berik Visschers on 2013-03-22. | |
// Copyright (c) 2013 Xaton. All rights reserved. | |
// | |
#import "UIResponder+sendAction.h" | |
@implementation UIResponder (sendAction) | |
- (BOOL)sendAction:(SEL)action { | |
return [self sendAction:action forEvent:nil]; | |
} | |
- (BOOL)sendAction:(SEL)action forEvent:(UIEvent *)event { | |
return [self sendAction:action to:self.nextResponder forEvent:event]; | |
} | |
- (BOOL)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event { | |
return [self sendAction:action to:target from:self forEvent:event]; | |
} | |
- (BOOL)sendAction:(SEL)action to:(id)target from:(id)sender forEvent:(UIEvent *)event { | |
UIResponder *implementor = target; | |
while ([implementor respondsToSelector:action] == NO) { | |
implementor = [implementor nextResponder]; | |
if (implementor == nil) { | |
return NO; | |
} | |
} | |
// [[UIApplication sharedApplication] sendAction:action to:implementor from:sender forEvent:event]; | |
#pragma clang diagnostic push | |
#pragma clang diagnostic ignored "-Warc-performSelector-leaks" | |
NSMethodSignature *methodSignature = [implementor methodSignatureForSelector:action]; | |
switch (methodSignature.numberOfArguments) { | |
case 2: | |
[implementor performSelector:action]; | |
break; | |
case 3: | |
[implementor performSelector:action withObject:sender]; | |
break; | |
case 4: | |
[implementor performSelector:action withObject:sender withObject:event]; | |
break; | |
default: | |
NSAssert(NO, @"Maximum two arguments are supported"); | |
break; | |
} | |
#pragma clang diagnostic pop | |
return YES; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment