Introduction:
In the previous article, we talked about KVC. In this article, we learned KVO. The full name is: Key Value Observing. the literal translation is: Key Value-based observer.
So what is its use? KVO is mainly used for view interaction. For example, if some data on the interface changes and the display of the interface also changes, it is necessary to establish a correlation between data and the interface.
KVO provided in ObjC solves this problem. The following example shows how to use KVO by observing the change of course name on the displayed page.
The Student class is named Student and the page class is PageView.
From the official website of Apple, the BankObject in the figure is like PageView, And the PersonObject is like Student,
PageView observe the changes of Student.
1. Add Student class.
. H
[Cpp]
# Import <Foundation/Foundation. h>
@ Interface Student: NSObject
{
NSString * name;
NSString * courseName;
}
-(Void) changeCourseName :( NSString *) newCourseName;
@ End
The class includes name and Course name courseName. Add a changeCourseName method that can change the course name. For comparison later, check whether there will be a callback when the course name is changed directly.
Implementation file. m
[Cpp]
# Import "Student. h"
@ Implementation Student
-(Void) changeCourseName :( NSString *) newCourseName
{
CourseName = newCourseName;
}
@ End
The implementation class implements the method.
2. Page class implementation
. H file
[Cpp]
# Import <Foundation/Foundation. h>
@ Class Student;
@ Interface PageView: NSObject
{
Student * student;
}
-(Id) init :( Student *) initStudent;
@ End
. M file
[Cpp]
# Import "PageView. h"
# Import "Student. h"
@ Implementation PageView
-(Id) init :( Student *) initStudent
{
If (self = [super init]) {
Student = initStudent;
[Student addObserver: self
ForKeyPath: @ "courseName"
Options: NSKeyValueObservingOptionOld
| NSKeyValueObservingOptionNew context: nil];
}
Return self;
}
-(Void) dealloc {
[Student removeObserver: self forKeyPath: @ "courseName" context: nil];
[Super dealloc];
}
-(Void) observeValueForKeyPath :( NSString *) keyPath
OfObject :( id) object
Change :( NSDictionary *) change
Context :( void *) context
{
If ([keyPath isEqual: @ "courseName"]) {
NSLog (@ "PageView course changed ");
NSLog (@ "the new course of PageView is: % @ the old course is: % @", [change objectForKey: @ "new"], [change objectForKey: @ "old"]);
}
}
@ End
When init is initialized, add the observer to the student instance and remove the observer when it is released.
3. Observation
In the main function
[Cpp]
# Import "Student. h"
# Import "Course. h"
# Import "PageView. h"
Int main (int argc, const char * argv [])
{
@ Autoreleasepool {
Student * student = [[Student alloc] init] autorelease];
[Student changeCourseName: @ "math"];
NSLog (@ "Initial Value: % @", [student valueForKey: @ "courseName"]);
// Create a page instance
PageView * pageview = [[PageView alloc] init: student] autorelease];
[Student setValue: @ "Chemistry Course" forKey: @ "courseName"];
}
Return 0;
}
Create a student instance, set the course to a mathematics class, and initialize it with student when creating the page class. This is a page class that has been observed by students.
Set a new value for the course to chemistry. Run the following command to print the result:
16:29:21. 561 objectiveC [2192: 403] Initial Value: Mathematics Class
16:29:21. 565 objectiveC [2192: 403] PageView course changed
16:29:21. 566 objectiveC [2192: 403] the new course PageView is: the old course of chemistry is: Mathematics
You can see that the callback in the Pageview class is called, and Pageview receives the student course data update information.
4. directly change the course information comparison
[Cpp]
# Import "Student. h"
# Import "Course. h"
# Import "PageView. h"
Int main (int argc, const char * argv [])
{
@ Autoreleasepool {
Student * student = [[Student alloc] init] autorelease];
[Student changeCourseName: @ "math"];
NSLog (@ "Initial Value: % @", [student valueForKey: @ "courseName"]);
// Create a page instance
PageView * pageview = [[PageView alloc] init: student] autorelease];
[Student setValue: @ "Chemistry Course" forKey: @ "courseName"];
[Student changeCourseName: @ "English class"];
NSLog (@ "The directly changed course is: % @", [student valueForKey: @ "courseName"]);
}
Return 0;
}
Author: totogo2010