美文网首页
KVC/KVO/NSNotificationCenter

KVC/KVO/NSNotificationCenter

作者: 阿龍飛 | 来源:发表于2016-08-23 16:32 被阅读37次

    KVC(key-value coding)

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        _objectb = [[ObjectB alloc]init];
        _objectb.str = @"sss";
        [_objectb addObserver:self forKeyPath:@"str" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
        _objectb.str = @"sss2";
    }
    
    // 第二步,实现接收通知的接口方法
    - (void)observeValueForKeyPath:(NSString *)keyPath
                          ofObject:(id)object
                            change:(NSDictionary<NSKeyValueChangeKey,id> *)change
                           context:(void *)context {
        // 这里最好判断一下object的类型和keyPath的值,不符合则交给父类处理
        if ([object isKindOfClass:[ObjectB class]] && [keyPath isEqualToString:@"str"]) {
            NSLog(@"%@", change);  // 这里可以读取到 new = 999
        } else {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    }
    
    // 第三步,移除观察者。
    - (void) dealloc {
        [_objectb removeObserver:self forKeyPath:@"str"];
    }
    
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        _objectb.str = @"999";
    }
    
    

    KVO可以在MVC模式中得到很好的应用。因为当Model发生变化时,通过KVO可以很方便地通知到Controller,从而通过Controller来改变View的展示。所以说 KVO是解决Model和View同步的好办法。



    NSNotificationCenter

    1、创建通知,最好在viewDidLoad的方法中创建

    - (void)viewDidLoad {
        [super viewDidLoad];
        [[NSNotificationCenter defaultCenter] addObserver:self   selector:@selector(tongzhi:) name:@"tongzhi" object:nil];
    }
    
    - (void) tongzhi:(NSNotification *)notification{
        NSLog(@"通知过来的 - dic = %@",notification.object);
    }
    
    

    2、发送通知

        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"324234",@"bankId",@"某某银行",@"bankName", nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"tongzhi" object:dic];
    

    3、移除通知,由那个控制器创建由那个控制器移除,谁创建谁移除,最好在dealloc方法中移除,

    -(void)dealloc{
         //第一种方法.这里可以移除该控制器下的所有通知
         // 移除当前所有通知
        NSLog(@"移除了所有的通知");
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    
         //第二种方法.这里可以移除该控制器下名称为tongzhi的通知
        //移除名称为tongzhi的那个通知
        NSLog(@"移除了名称为tongzhi的通知");
        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"tongzhi" object:nil];
    }
    

    系统自带的通知有:

    UIDevice通知...
    UIDeviceOrientationDidChangeNotification // 设备旋转
    UIDeviceBatteryStateDidChangeNotification // 电池状态改变
    UIDeviceBatteryLevelDidChangeNotification // 电池电量改变
    UIDeviceProximityStateDidChangeNotification // 近距离传感器(比如设备贴近了使用者的脸部)

    键盘通知...

    //应用程序进入到后台的通知
    //UIApplicationDidEnterBackgroundNotification 程序进图后台的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationGotoBackGround) name:UIApplicationDidEnterBackgroundNotification object:nil];
    //接受到键盘frame改变的通知
    //UIKeyboardWillChangeFrameNotification 键盘frame将要改变的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
    

    相关文章

      网友评论

          本文标题:KVC/KVO/NSNotificationCenter

          本文链接:https://www.haomeiwen.com/subject/ezqnsttx.html