你好,游客 登录 注册 搜索
背景:
阅读新闻

Objective-C语法之KVO的使用

[日期:2012-07-26] 来源:blog.csdn.net/totogo2010  作者:totogo2010 [字体:大 中 小]

4、直接改变课程信息对比

  1. #import "Student.h"   
  2. #import "Course.h"   
  3. #import "PageView.h"   
  4.   
  5. int main(int argc, const char * argv[])  
  6. {  
  7.     @autoreleasepool {  
  8.         Student *student = [[[Student alloc]init]autorelease];  
  9.         [student changeCourseName:@"数学课"];  
  10.         NSLog(@"初始值:%@", [student valueForKey:@"courseName"]);  
  11.           
  12.         //创建页面实例   
  13.         PageView *pageview = [[[PageView alloc]init:student]autorelease];  
  14.           
  15.         [student setValue:@"化学课" forKey:@"courseName"];  
  16.         [student changeCourseName:@"英语课"];  
  17.         NSLog(@"直接改变的课程为:%@", [student valueForKey:@"courseName"]);  
  18.   
  19.     }  
  20.     return 0;  
  21. }  

直接调用changeCourseName方法改变课程,打印结果:


2012-07-24 16:32:06.230 objectiveC[2240:403] 初始值:数学课

2012-07-24 16:32:06.237 objectiveC[2240:403] PageView课程被改变了

2012-07-24 16:32:06.238 objectiveC[2240:403] PageView新课程是:化学课老课程是:数学课

2012-07-24 16:32:06.239 objectiveC[2240:403] 直接改变的课程为:英语课

可以看到,这时Pageview的回调没被调用到。说明只有通过键值编码(KVC)改变的值,才会回调观察者注册的方法。

这里是苹果官网的关于KVO的文档,英文好的朋友可以看看:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html#//apple_ref/doc/uid/10000177-BCICJDHA

linux
相关资讯       Objective-C 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款