IOS開発学習31 ObjectCカスタムEventを実現


クラス間通信は、Notificationを使用してもよいし、Eventを使用してもよい.ここでEventを実装する方法を記録します.まず、定義されたクラスをUIcontrolから継承します.
SocketUtilsクラスサブコントロール
SocketUtilsヘッダファイル

#import <UIKit/UIKit.h>
#import "AsyncSocket.h"

@interface SocketUtils : UIControl{

}

enum {
    UIControlEventAlert=0x00000001<<25, //       24  27
    UIControlEventError=0x00000001<<26
};

SocketUtils.mファイル
イベントをトリガーする方法:
[self sendActionsForControlEvents:UIControlEventAlert];

SocketUtilsのターゲットクラスの使用
ヘッダファイル
#import <UIKit/UIKit.h>
#import "WasherSocketUtils.h"
@interface Head : UIView
@property (nonatomic,strong)SocketUtils* SocketUtils;

mファイル
#import "Head.h"

@implementation Head
@synthesize SocketUtils=_SocketUtils;
-(id)init{
    _SocketUtils=[[SocketUtils alloc]init];
    ///    ,     
    [_SocketUtils addTarget:self action:@selector(socketEvent:) forControlEvents:UIControlEventAlert];

    return [super init];
}
-(void)socketEvent:(SocketUtils*)paramSender{
    NSLog(@"event by xie:%@",paramSender);
}

viewcontrollerを終了するときは、イベント登録を削除したほうがいいです.
-(void)viewWillDisappear:(BOOL)animated{
    [_SocketUtils removeTarget:self action:@selector(socketEvent:) forControlEvents:UIControlEventAlert];
}