IOS: notification & KVO in message communication

Source: Internet
Author: User
Tags notification center

In IOs: MVC, I posted a classic image:

 

What is the noification and KVO of model communication to the controller?

In terms of function, the delegate, notification, and KVO functions are similar and act on message communication of objects in OC. However, the application scenarios of the three methods are different. To put it simply, delegate is a type of return function, which is more used in one-to-one scenarios. For details, refer to iPhone: Delegate mechanism. Less notification is used, and notification center is used, similar to the broadcast method, it is more suitable for one-to-many communication. KVO, key-value-observing, and observer mode are suitable for listening for changing attributes of another object.

The detailed difference between the three can refer to another blog: http://mobile.51cto.com/iphone-386316.htm

Notification:

The use of notification is very simple.Code:

 

     //  Obtain instances using class methods Nsicationicationcenter * center = [Nsnotificationcenter defacenter center];  //  Two message listeners are added. The message names are loginfo. The method getinfo is used to listen to messages in the current object, and the method oneobjhandleinfo is used to listen to messages in the oneobj object. [Center addobserver: Self selector: @ selector (getinfo :) name: @"  Loginfo  "   Object  : Nil]; [center addobserver: oneobj selector: @ selector (oneobjhandleinfo :) name:  @" Loginfo  "   Object  : Nil];  //  Sends a message. The message name is loginfo, and the data is transmitted as an nsstring. [Center postnotificationname: @"  Loginfo  "   Object : @"  00000  " ];

 

Corresponding two receiving methods:

 // In this object... -( Void ) Getinfo :( nsnotification * ) Notificaion {  //  Obtain and print accepted data Nsstring * Data = [notificaion Object  ]; Nslog (  @"  >>% @  "  , Data );}  //  In the oneobj object... -( Void ) Oneobjhandleinfo :( nsnotification *) Notification {  //  Obtain and print accepted data Nsstring * Data = [Notification Object  ]; Nslog (  @"  > Oneobj % @  "  , Data );} 

In this way, when a message is post, the corresponding two listeners can receive the message and perform relevant processing. Finally, you must remove the corresponding listener when you do not need it.

[Center removeobserver: Self Name:@"Loginfo" Object: Nil]; [center removeobserver: oneobj Name:@"Loginfo" Object: Nil];

KVO:

Before reading KVO, it is necessary to first understand KVC, that is, key-value coding key-Value Pair programming. You can use key-value to conveniently access data.

The specific operation is as follows: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:  @"  100 " 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 adopts the observer mode:

Book4 =[[Book alloc] init];//Add the observer. For this class, keypath is the price object in book, so it is the price[Book4 addobserver: Self forkeypath:@"Price"Options: nskeyvalueobservingoptionold |Nskeyvalueobservingoptionnew context: Nil];//Modify Value[Book4 setvalue:@"4"Forkey:@"Price"];
 //  Return 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 is % @  " , [Change objectforkey: @"  Old  "  ]); Nslog (  @" New price is % @  " , [Change objectforkey: @"  New  "  ]) ;}} 

In this way, the corresponding response is made when the object property is changed.

 More detailed implementation of KVO can also refer to: http://blog.csdn.net/yuquan0821/article/details/6646400

How is KVC and KVO implemented internally?

"When an object calls setvalue, (1) first, find the environment parameters required for running the method based on the method name. (2) he will use his ISA pointer to combine environment parameters to find the specific method to implement the interface. (3) directly find the specific method implementation.

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 accessed key is the observed key, we can easily find the observer object in the Observer registry and send a message to it ."

Detailed check: http://www.cocoadev.cn/Cocoadev/KVO-20100222-0627.asp

 

The OC communication notifications and KVO in IOS are probably the same.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.