Last active
April 15, 2016 08:07
-
-
Save edom18/61acc044e780dfe0644dd95f20a273e4 to your computer and use it in GitHub Desktop.
[Objective-C] Responder Chainを利用して親ビューにイベントを届ける ref: http://qiita.com/edo_m18/items/b55308c06a8267b3ea5f
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
// メソッド名がかぶって誤動作しないようにプロトコルにする | |
@protocol AnyEvent <NSObject> | |
@optional | |
- (void)tappedButton1:(id)responder; | |
- (void)tappedButton2:(id)responder; | |
@end | |
////////////////////////////////////////////////// | |
/** | |
* ボタンを押下した際のイベントハンドラ | |
*/ | |
- (void)tappedButton1:(id)sender | |
{ | |
PassResponderChain(tappedButton1:, self, AnyEvent); | |
} |
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
[UIApplication.sharedApplication sendAction:@selector(action:) | |
to:nil | |
from:self | |
forEvent:nil]; |
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
// イベントを透過する処理をマクロ化しておく | |
#define PassResponderChain(sel, sender, proto) \ | |
id next = self;\ | |
do {\ | |
next = [((UIResponder*)next).nextResponder targetForAction:@selector(sel)\ | |
withSender:sender];\ | |
if (next) {\ | |
if ([next conformsToProtocol:@protocol(proto)]) {\ | |
[next sel sender];\ | |
break;\ | |
}\ | |
}\ | |
} while (next) |
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
@interface ViewController <AnyEvent> | |
// ... 中略 | |
@end | |
@implementation ViewController | |
/** | |
* ボタンイベントをトラッキング | |
*/ | |
- (void)tappedButton1:(id)sender | |
{ | |
// do anything. | |
// さらに上に伝播させる | |
PassResponderChain(tappedButton1:, sender, AnyEvent); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment