Created
October 27, 2012 23:21
-
-
Save tadamatu/3966823 to your computer and use it in GitHub Desktop.
Objective-c Event
This file contains hidden or 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 { | |
//イベント時の処理を追加 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment