IOS开发学习31 ObjectC 实现自定义Event
2015-06-03 17:08
531 查看
跨类的通讯,可以使用Notification,也可以使用Event。这里记录一下实现Event的方法:
首先,定义的类要从UIControl继承。
在退出viewcontroller的时候,最好把事件注册移除。
首先,定义的类要从UIControl继承。
SocketUtils类 子control
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]; }
相关文章推荐
- 用NSCoding协议完成“编码/解码”操作-Object-C
- Objective-C Runtime
- [Objective-C]关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- Swift项目兼容Objective-c问题汇总
- Javascript与Objective-C 字符串与数组的方法类比
- Objective-c之Singletone模式
- iOS开发(Objective-C)常用库索引
- 中兴手机使用webview出现NPObject deleted问题
- Objective-C中的instancetype和id关键字
- Object-C 声明属性为什么用下划线,代码规范和编程风格
- Objective-C学习——中文URL编码和解码
- Objective-C编码规范:26个方面解决iOS开发问题
- PHP“Cannot use object of type stdClass as array”
- [iOS] performSelector:withObject:afterDelay:调用没有作用的问题及解决方法
- object_getClassName swift得到类名
- Could not import Objective-C header
- PHP Object对象转换为Array数组
- a different object with the same identifier value was already associated with异常解决
- Objective-C 基础教程 正式协议
- Compare .NET Objects对象比较组件