// KVC: Key Value Coding, 常见作用:给模型属性赋值
    // KVO: Key Value Observing, 常用作用:监听模型属性值的改变

 //
// ViewController.m
// 11-通知、KVO、代理
//
// Created by xiaomage on 15/6/6.
// Copyright (c) 2015年 xiaomage. All rights reserved.
// #import "ViewController.h"
#import "XMGPerson.h" @interface ViewController ()
@property (nonatomic, strong) XMGPerson *p1;
@property (nonatomic, strong) XMGPerson *p2;
@property (nonatomic, strong) XMGPerson *p3;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; // self.p1 = [[XMGPerson alloc] init];
// self.p1.name = @"p1";
//
// self.p2 = [[XMGPerson alloc] init];
// self.p2.name = @"p2";
//
// self.p3 = [[XMGPerson alloc] init];
// self.p3.name = @"p3";
// 同一个通知TestNotification可以被多个对象person监听
// 当收到TestNotification通知就调用self.p1的test方法
// [[NSNotificationCenter defaultCenter] addObserver:self.p1 selector:@selector(test) name:@"TestNotification" object:nil];
// [[NSNotificationCenter defaultCenter] addObserver:self.p2 selector:@selector(test) name:@"TestNotification" object:nil];
// [[NSNotificationCenter defaultCenter] addObserver:self.p3 selector:@selector(test) name:@"TestNotification" object:nil];
// 多个对象可以发出同一个通知
//对象@“123”发出TestNotification通知
// [[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification" object:@"123"];
// [[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification" object:@"345"]; // KVC: Key Value Coding, 常见作用:给模型属性赋值
// KVO: Key Value Observing, 常用作用:监听模型属性值的改变 self.p1 = [[XMGPerson alloc] init];
self.p1.name = @"p1";
//self.p1 监听属性 name的改变,NSKeyValueObservingOptionOld表示监听方法中的NSDictional显示旧值
[self.p1 addObserver:self forKeyPath:@"name" options: NSKeyValueObservingOptionOld context:nil]; self.p1.name = @"pppp1";
}
//所有的通知监听 都要在dealloc销毁时removeObserver
- (void)dealloc
{
[self.p1 removeObserver:self forKeyPath:@"name"];
} #pragma mark - KVO监听方法
/**
* 当监听到object的keyPath属性发生了改变
*这个方法是NSObject的类扩展方法,用来监听对象属性改变
*keyPath属性名称如name
*id 对象 如self.p1
*change 记录改变前后的值
*/
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"监听到%@对象的%@属性发生了改变, %@", object, keyPath, change);
} @end

ios通知-kvo的更多相关文章

  1. IOS中KVO模式的解析与应用

    IOS中KVO模式的解析与应用 最近老翁在项目中多处用到了KVO,深感这种模式的好处.现总结如下: 一.概述 KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修 ...

  2. iOS通知的整理笔记

    iOS通知用于高耦合界面的传值确实方便快捷. 需要实现模态弹出的视图控制器上,有一个视图控制器可以导航.这必定要将这个视图控制器的导航视图控制器naVC.view添加到模态弹出的视图控制器presen ...

  3. iOS下KVO使用过程中的陷阱 (转发)

    iOS下KVO使用过程中的陷阱   KVO,全称为Key-Value Observing,是iOS中的一种设计模式,用于检测对象的某些属性的实时变化情况并作出响应.网上广为流传普及的一个例子是利用KV ...

  4. iOS 通知的变化ios9-10,新功能展示

    二.新功能展示 1  使用 /iOS通知新功能玩法 2.  全面   iOS10里的通知与推送详情 一.变化 四.Notification(通知) 自从Notification被引入之后,苹果就不断的 ...

  5. iOS通知中心

    iOS通知中心 它是iOS程序内部的一种消息广播机制,通过它,可以实现无引用关系的对象之间的通信.通知中心他是基于观察者模式,它只能进行程序内部通信,不能跨应用程序进程通信. 当通知中心接受到消息后会 ...

  6. delphi IOS 通知 TNotification

    delphi  IOS 通知 TNotification http://blogs.embarcadero.com/ao/2013/05/01/39450 TNotification http://d ...

  7. iOS 通知观察者的被调函数不一定运行在主线程

    Tony in iOS | 08/08/2013 iOS 通知观察者的被调函数不一定运行在主线程 今天修复Bug时候发现的一个小细节,记录下. 问题描述 事情是这样的:我在A视图(UITableVie ...

  8. iOS 通知扩展插件

    iOS 通知扩展插件 目录 iOS 通知扩展插件 Notification Service Extension 新建一个target 代码实现 注意事项 UINotificationConentExt ...

  9. iOS通知的使用

    注册:[[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:self]; ...

随机推荐

  1. The type or namespace name '****' could not be found (are you missing a using directive or an assembly reference

    错误的提升内容:

  2. HNU OJ10320 穿越火线 简单模拟

    穿越火线 Time Limit: 10000ms, Special Time Limit:25000ms, Memory Limit:65536KB Total submit users: 12, A ...

  3. class0513(html基础加强)

    内容:HTML.CSS 目标:掌握手写HTML实现一般难度的Web页面的能力(如网站注册表单),为ASP.Net学习打基础.坚持手写HTML,可视化设计只是一种自学的手段. 参考书:张孝祥<Ja ...

  4. 恒天云技术分享系列4 – OpenStack网络攻击与防御

    恒天云技术分享系列:http://www.hengtianyun.com/download-show-id-13.html 云主机的网络结构本质上和传统的网络结构一致,区别大概有两点. 1.软网络管理 ...

  5. android-serialport-api and libusb for android

    libusb for android: Even if you get it compiled, Android is probably not going to let you access the ...

  6. 【组队训练】2016 ACM/ICPC Asia Regional Dalian Online

    因为不是一队……毫无晋级的压力……反正有压力也进不去呵呵呵…… 开场zr看1006我看1010.. 1010我一直在wa... zr的1006倒是比较轻松的过了...然后我让他帮我看10.... 跟他 ...

  7. 将NavigationBar设置透明

    将NavigationBar设置透明(仅将指定视图控制器进行透明处理),步骤如下:1.在视图控制器的头文件中实现UINavigationControllerDelegate,例如:@interface ...

  8. iOS block的用法

    本章学习目标: 1. 了解何谓block. 2. 了解block的使用方法. Block 是iOS在4.0之后新增的程式语法,严格来说block的概念并不算是基础程式设计的范围,对初学者来说也不是很容 ...

  9. Activity详解

    Activity是android应用的重要组成单元之一(另外3个是Service,BroadcastReceiver和ContentProvider).实际应用包含了多个Activity,不同的Act ...

  10. Visual Studio 2012 应用软件开发新方式

    微软正式发布Visual Studio 2012 应用软件开发新方式 2012-09-13 09:54 51CTO.com 我要评论(0) 字号:T | T “现在,开发者将有更好的机会开发与云服务连 ...