Skip to content

Instantly share code, notes, and snippets.

@tadamatu
Created October 27, 2012 23:21
Show Gist options
  • Save tadamatu/3966823 to your computer and use it in GitHub Desktop.
Save tadamatu/3966823 to your computer and use it in GitHub Desktop.
Objective-c Event
/* ========================================
イベント実装のサンプル
xxx.h/xxx.m イベント通知側
yyy.h/yyy.m イベント受け側
======================================== */
/* ========================================
Step1.プロトコルを定義(xxx.h はたは 別ファイル)
======================================== */
@protocol aaaDelegate <NSObject>
@optional
-(void)aaaEvents:(NSString *)text ;
@end
/* ========================================
Step2.delegateを追加(xxx.h & xxx.m)
======================================== */
//(xxx.h)プロパティでdelegateを用意
@interface xxxView : UIView {
id<aaaDelegate> _delegate;
}
@property (nonatomic, assign) id<aaaDelegate> delegate;
//(xxx.m)プロパティとして追加
@synthesize delegate = _delegate;
/* ========================================
Step3.任意の場所でEventを返却(xxx.m)
======================================== */
if ([_delegate respondsToSelector:@selector(aaaEvents:)]){
[_delegate aaaEvents:_text];
}
/* ========================================
Step4.利用する(yyy.h & yyy.m)
======================================== */
//(yyy.h)使用するクラス名にProtocolを追加
@interface yyyView : UIView<aaaDelegate>
//(yyy.m)自信にDelegate追加
- (void)loadView {
xxxView.delegate = self;
}
//(yyy.m)イベント
-(void)aaaEvents:(NSString *)text {
//イベント時の処理を追加
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment