`

cocoa's Key-value coding

 
阅读更多

Key-value coding(KVC)机制允许set(设置)和get(获取)变量值。这里的变量名可能是一个字符串,也就是Key。例如,类Company拥有一个类型为NSString,叫做companyName的变量。

我们就可以这样设置和获取Company实例的companyName值:


实例setValue:forKey和valueForKey:的方法在NSObject中有定义。

创建名为KVCFun的项目,新建名为AppController的Objective-C Class文件。

AppController.h和AppController.m的代码分别如下:



打开MainMenu.nib,添加一个Slider、一个Label、一个Button控件,如下图:

将Slider的Attributes Inspector->Control->State设为“连续的”,再把Binding Inspector->value邦定到AppController实例的fido key上。

将Label也邦定到AppController上,Model Key Path设为fido。

Button链接到incrementFido:action上。

@property和@synthesize

我们可以使用property来代替fido和setFido,并且使用synthesize来实现存取方法。


使用下面的代码替换AppController.h中的fido和setFido:

使用@synthesize来替换fido和setFido,程序可以正常运行。

分享到:
评论

相关推荐

    Cocoa.Programming.for.Mac.OS.X,3rd,2008(中文,仅18章).pdf

    第七章:Key-Value Coding.Key-Value Observing 第八章:NSArrayController 第九章:NSUndoManager 第十章:Archiving 第十一章:Core Data 基本原理 第十二章:Nib 文件和 NSWindowController 第十三章:User ...

    iOS 7 Programming Fundamentals: Objective-C, Xcode, and Cocoa Basics

    iOS 7 Programming Fundamentals: ...Learn the role of accessors, key-value coding, and properties Understand the power of ARC-based object memory management Send messages and data between Cocoa objects

    [iPhone开发书籍大全].For.Dummies.Cocoa.Programming.for.Mac.OS.X.For.Dummies(Wiley.2009-2).pdf

    Simplify with key-value coding The better you understand Cocoa programming, the better the applications you can create for Mac OS X, iPhone, and iPod Touch. Cocoa Programming for Mac OS X For Dummies...

    iOS 7 Programming Fundamentals

    Explore Cocoa’s use of Objective-C linguistic features, Use Cocoa’s event-driven model and major design patterns, Learn the role of accessors, key-value coding, and properties, Understand the power...

    核心动画高级技巧

    你还应该熟悉键-值编程方法(参考文档 Key-Value Coding Programming Guide)。同时如果你对Quartz2D编程有一定了解的话,将会对您掌握 本文档带来帮助,但这不是必须的(参考文档 Quartz 2D Programming Guide)

    核心动画编程指南

    你还应该熟悉键-值编程方法(参考文档 Key-Value Coding Programming Guide)。同时如果你对Quartz2D编程有一定了解的话,将会对您掌握 本文档带来帮助,但这不是必须的(参考文档 Quartz 2D Programming Guide)。

    iOS 核心动画编程

    你还应该熟悉键-值编程方法(参考文档 Key-Value Coding Programming Guide)。同时如果你对Quartz2D编程有一定了解的话,将会对您掌握 本文档带来帮助,但这不是必须的(参考文档 Quartz 2D Programming Guide)。 您...

    [iOS] iOS 7 编程 (英文版)

    Pick up iOS 7 Programming Fundamentals to learn about Objective-C, Xcode, and Cocoa language features such as notifications, delegation, memory management, and key-value coding. Together with ...

    IPhone中文开发文档

    什么是cocoa 起步 Objective—C语言 内存管理 Target/Action 辅助对象 Key—Value Coding ,Key—Value Observing NSArrayControler NsUndoManager Archiving Coredata基本原理 User Default

    Programming iOS 11_Dive Deep into Views,Controllers, and Frameworks 2018.pdf

    and architectures of the Cocoa Touch framework (including delegation, the responder chain, key–value coding, key–value observing, memory management, and so on), has been relegated to iOS 11 ...

    Python Cookbook, 2nd Edition

    Associating Multiple Values with Each Key in a Dictionary Recipe 4.16. Using a Dictionary to Dispatch Methods or Functions Recipe 4.17. Finding Unions and Intersections of Dictionaries Recipe ...

Global site tag (gtag.js) - Google Analytics