zoukankan      html  css  js  c++  java
  • KVO(1)

    #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"]);
        }
    }
    
  • 相关阅读:
    华为机考--约瑟夫问题
    华为机考--字符串压缩
    2014华为校招机考模拟--求最大递增数
    2014华为校招机考模拟--记票统计
    插入排序与归并排序的C#实现
    步入C#--hello world
    shell 命令用法
    About compiling some source code on centos
    ubuntu Install packages
    ssh命令
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/7243023.html
Copyright © 2011-2022 走看看