実装方法
基本は次の4ステップStep1.プロトコルを定義
Step2.delegateを追加
Step3.任意の場所でEventを返却
Step4.利用する
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
/* ======================================== | |
イベント実装のサンプル | |
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 { | |
//イベント時の処理を追加 | |
} |