设为首页 加入收藏

TOP

ObjectC 实现自定义Event
2015-07-16 12:04:16 来源: 作者: 【 】 浏览:81
Tags:ObjectC 实现 定义 Event

跨类的通讯,可以使用Notification,也可以使用Event。这里记录一下实现Event的方法:
首先,定义的类要从UIControl继承。

SocketUtils类 子control

SocketUtils头文件


#import 
   
     #import "AsyncSocket.h" @interface SocketUtils : UIControl{ } enum { UIControlEventAlert=0x00000001<<25, //可以自定义的从24 到27 UIControlEventError=0x00000001<<26 }; 
   

SocketUtils.m文件

触发事件的方法:

[self sendActionsForControlEvents:UIControlEventAlert];

使用SocketUtils的目标类

头文件

#import 
   
     #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);
}
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言数组a[i]==i[a] 下一篇数据结构(C实现)------- 最小生..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: