Skip to content

Instantly share code, notes, and snippets.

@edom18
Last active April 15, 2016 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edom18/61acc044e780dfe0644dd95f20a273e4 to your computer and use it in GitHub Desktop.
Save edom18/61acc044e780dfe0644dd95f20a273e4 to your computer and use it in GitHub Desktop.
[Objective-C] Responder Chainを利用して親ビューにイベントを届ける ref: http://qiita.com/edo_m18/items/b55308c06a8267b3ea5f
// メソッド名がかぶって誤動作しないようにプロトコルにする
@protocol AnyEvent <NSObject>
@optional
- (void)tappedButton1:(id)responder;
- (void)tappedButton2:(id)responder;
@end
//////////////////////////////////////////////////
/**
* ボタンを押下した際のイベントハンドラ
*/
- (void)tappedButton1:(id)sender
{
PassResponderChain(tappedButton1:, self, AnyEvent);
}
[UIApplication.sharedApplication sendAction:@selector(action:)
to:nil
from:self
forEvent:nil];
// イベントを透過する処理をマクロ化しておく
#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)
@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