跨類的通訊,可以使用Notification,也可以使用Event。這裡記錄一下實現Event的方法:
首先,定義的類要從UIControl繼承。
#import
#import "AsyncSocket.h"
@interface SocketUtils : UIControl{
}
enum {
UIControlEventAlert=0x00000001<<25, //可以自定義的從24 到27
UIControlEventError=0x00000001<<26
};
觸發事件的方法:
[self sendActionsForControlEvents:UIControlEventAlert];
#import
#import "WasherSocketUtils.h"
@interface Head : UIView
@property (nonatomic,strong)SocketUtils* SocketUtils;
#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);
}