KVO(1)
2015-06-30 13:04
369 查看
#import "ViewController.h" @interface ViewController () @property(nonatomic, strong)UIButton *button; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.button = [[UIButton alloc] initWithFrame:CGRectMake(30, 50, 50, 30)]; [self.button setTitle:@"测试" forState:UIControlStateNormal]; [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; self.button.layer.borderWidth = 1.0f; [self.view addSubview:self.button]; //注册监听button的enabled状态 [self.button addObserver:self forKeyPath:@"enabled" options:NSKeyValueObservingOptionNew context:@"test_button"]; // 3秒钟后改变当前button的enabled状态 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.button.enabled = YES; }); // Do any additional setup after loading the view, typically from a nib. } /** * 监听按钮状态改变的方法 * * @param keyPath 按钮改变的属性 * @param object 按钮 * @param change 改变后的数据 * @param context 注册监听时context传递过来的值 */ -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { UIButton *button = (UIButton *)object; if (self.button == button && [@"enabled" isEqualToString:keyPath]) { NSLog(@"self.button的enabled属性改变了%@",[change objectForKey:@"new"]); } }
相关文章推荐
- nyoj 202 红黑树 【红黑树性质 旋转后中序序列不变】 【二叉树中序遍历】
- IIS配置CI框架.htaccess通用配置
- RapidJSON 代码剖析(四):优化 Grisu
- 使用AIDL实现进程间的通信
- 界面库-分类对比
- mybatis 一对一查询
- VC中调用调试自己项目的DLL
- 一个按比特位拷贝数据的函数copybits
- Mac系统之----教你怎么显示隐藏文件,或者关闭显示隐藏文件
- SAT数学试题考查范围
- 飞机机身结构主要分三种,现在大部分飞机都用第三种
- 设计模式学习之——命令模式
- Linux中常用操作命令
- Python seek函数
- 进程间通信--无名管道
- shell学习三十三天----关于重定向
- Welcome to OWASP CHINA — OWASP-CHINA
- oracle分页查询
- oracle 表空间
- JavaWeb学习笔记——开发动态WEB资源(四)打印当前使用的是get方法