zoukankan      html  css  js  c++  java
  • [IOS学习笔记]KVO

    //属性
    @property(nonatomic) BOOL isFinished;
    //注册监听 [self addObserver:self forKeyPath:@"isFinished" options:0 context:NULL];
    //响应变更事件 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqualToString:@"isFinished"]) { NSLog(@"changeeddd"); } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } }
    //变更属性数值 [button addTarget:self action:@selector(changeFinished:) forControlEvents:UIControlEventTouchUpInside]; -(void)changeFinished:(id)sender { //自动通知 [self setValue:[NSNumber numberWithBool:YES] forKey:@"isFinished"]; }

    注册监听器参数 options

    NSKeyValueObservingOptionNew 表示属性未改变​之前的值;

    NSKeyValueObservingOptionOld 表示属性改变后的值;​

    移除观察者:

    [subject removeObserver:observer forKeyPath:@"name"]; 

    KVO相当于JAVA:

    account.setListener(new MyListener(){

        inspector.method();

    });

    但是不需要和JAVA一样声明一个MyListener接口,也不需要在account类中新建一个setListeber(MyListener listener);的方法,所以更灵活,但是变更属性时不能简单赋值!

  • 相关阅读:
    Python之面向对象知识整理
    python2与python3的区别
    Gitlab 删除仓库文件夹
    Git撤销本地commit(未push)
    js库
    HTML | 打开网址后3秒跳转外链
    Vue CLI | 安装
    npm | npm淘宝镜像和查看镜像设置
    swiper | 过渡效果 effect: 'fade' 导致文字重叠
    CSS改变背景 | pattern.css
  • 原文地址:https://www.cnblogs.com/afluy/p/4115234.html
Copyright © 2011-2022 走看看