IOS: NOTIFICATION&KVO in message communication
In the IOS:MVC, I posted a classic picture:
What is the noification&kvo of the model that communicates to the controller?
Functionally, the functions of delegate, notification, and KVO are similar, and all are message communication that acts on objects in OC. But the use of the three scenarios is different.
Simply said delegate is a kind of back-off function , more used in one-on occasions, can refer to the iphone:delegate mechanism;
Notification used less, using Notification Center, similar to broadcast mode, so more suitable for one-to-many communications;
Kvo,key-value-observing, the Observer pattern, is useful for listening to changes in the properties of another object.
The detailed difference between the three can refer to another blog post: http://mobile.51cto.com/iphone-386316.htm
Notification:
Notification is very simple to use, look directly at the code:
Use the class method to get the instance nsnotificationcenter *center = [Nsnotificationcenter defaultcenter]; Add 2 messages to listen, the message name is Loginfo, a listener in this object, listening method GetInfo; a listener in Oneobj object, monitoring method Oneobjhandleinfo. [Center addobserver:self selector: @selector (GetInfo:) name:@ "Loginfo" object:nil]; [Center Addobserver:oneobj selector: @selector (oneobjhandleinfo:) name:@ "Loginfo" object:nil]; Send message, message name Loginfo, pass data to a nsstring [center postnotificationname:@ "Loginfo" object:@ "00000"];
The corresponding 2 receive methods:
This object ...-(void) GetInfo: (nsnotification *) notificaion{ //Get Acceptance data and print nsstring *data = [Notificaion object]; NSLog (@ ">>%@", data);} Oneobj object ...-(void) Oneobjhandleinfo: (nsnotification*) notification{ //Get Acceptance data and print nsstring *data = [ Notification Object]; NSLog (@ ">>oneobj%@", data);}
This enables the post of a message, the corresponding 2 listeners will be able to receive the disappearance and related processing. The last note is to remove the corresponding listener when not in use.
[Center removeobserver:self name:@ "Loginfo" object:nil]; [Center removeobserver:oneobj name:@ "Loginfo" object:nil];
KVO:
Before looking at KVO, it is necessary to understand the next KVC, that is, Key-value Coding key-value pair programming. Data can be accessed conveniently through the key-value.
The specific operation is simply said: SetValue: Forkey:valueforkey:
Book Object//.h#import <Foundation/Foundation.h> @class Author; @interface book:nsobject{ nsstring *name; Author *author; float price; Nsarray *relativebooks;} @end//.m#import "Book.h" @implementation book@end
Book *book = [[Book alloc] init]; [Book setvalue:@ ' IOS book ' forkey:@ ' name ']; NSString *name = [book valueforkey:@ "name"]; NSLog (@ ">>%@", name); Author *author = [[Author alloc] init]; [Author setvalue:@ "Zhan" forkey:@ "name"]; [Book Setvalue:author forkey:@ "author"]; NSString *authorname = [book valueforkeypath:@ "Author.name"]; NSLog (@ ">>%@", authorname); [Book setvalue:@, "forkey:@", "Price"]; NSLog (@ ">>%@", [Book valueforkey:@ "Price"]); Book *book1 = [[Book alloc] init]; [Book1 setvalue:@ "4" forkey:@ "Price"]; Book *book2 = [[Book alloc] init]; [Book2 setvalue:@ "6" forkey:@ "Price"]; Nsarray *books = [Nsarray arraywithobjects:book1,book2,nil]; [Book Setvalue:books forkey:@ "Relativebooks"]; NSLog (@ ">>%@", [Book valueforkeypath:@ "Relativebooks.price"]);
More detailed KVC Introduction can refer to: HTTP://MARSHAL.EASYMORSE.COM/TECH/OBJC-%E4%BD%BF%E7%94%A8KVC
KVO is implemented based on KVC and takes the Observer's pattern:
Book4=[[book alloc] init]; To increase the observer, for this class, KeyPath is the price object in book, so for Price [book4 addobserver:self forkeypath:@ ' price ' options: nskeyvalueobservingoptionold| Nskeyvalueobservingoptionnew Context:nil]; Modified value [Book4 setvalue:@ "4" forkey:@ "Price"];
Fallback method-(void) Observevalueforkeypath: (NSString *) KeyPath ofobject: (ID) object Change : (Nsdictionary *) Change context: (void *) context{ NSLog (@ "cel-back"); if ([KeyPath isequal:@ "Price"]) { NSLog (@ ">>>>>>>price is changed"); NSLog (@ "Old Price was%@", [Change objectforkey:@ "old"]); NSLog (@ "New Price was%@", [Change objectforkey:@ "new"]);} }
This enables the corresponding response to be made when the object property is changed.
More detailed KVO implementation can also be referred to: http://blog.csdn.net/yuquan0821/article/details/6646400
How did the KVC and kvo interior be realized?
"When an object calls SetValue, (1) first finds the environment parameters required to run the method according to the method name. (2) He will combine the environment parameters from his own Isa pointer to find the specific method implemented by the interface. (3) The specific method can be directly found.
Because of the implementation mechanism of KVC, it is easy to see the key of a KVC operation, and then it is easy to match the key in the Observer registry. If the key being accessed is the observed key, then we can easily find the observer object in the Observer registry and send him a message inside. ”
Detail check:http://www.cocoadev.cn/cocoadev/kvo-20100222-0627.asp
The notifications and KVO for OC Communication in iOS are probably all that.
IOS: NOTIFICATION&KVO in message communication