4. KVO and KVO: Notification objective-C

Source: Internet
Author: User
Tags notification center

KVC

[Object setvalue: avalue forkey: akey]; // assign a value to the akey variable of the object

Avalue = [object valueforkey: akey]; // obtain the value of the akey variable of the object

[Object setvalue: avalue forkeypath: akeypath]; // set the value

Id avalue = [object valueforkeypath: akeypath]; // Value

Note: variables in keypath! Akeypath, such as P. Name (many points can be entered)

Demo:

Student * Stu = [[STUDENT alloc] init];

[STU setvalue: @ "Zhang San" forkey: @ "name"]; // assign values to object variables

Nsstring S1 = [STU valueforkey: @ "name"]; // retrieve the variable value

[STU release];

KVO

1. register the listener

[Addobserver: Listener

Forkeypath: attributes of the Monitored object

Options: monitoring content (new value or old value)

Context: Additional information];

2. Listener implementation listening method (implemented in the listener class, will crash if not implemented)

-(Void) observervalueforkeypath :( nsstring *) keypath ofobject :( ID) object (Monitored object) Change :( nsdictionary *) Change (Monitored object attribute change) Context :( void *) Context

3. Listener trigger conditions

When the properties of the Monitored object change, // The following method is automatically called.

Demo:

-(Void) observervalueforkeypath :( nsstring *) keypath ofobject :( ID) object change :( nsdictionary *) Change
Context :( void *) Context

{

Nslog :( @ "keypath: % @ object: % @ change: % @ context: % @", keypath, object, change, context ); // You can implement what you want to do in this method, such as sending notifications.

}

Student * Stu = [[STUDENT alloc] init];

Stu. Name = @ "James ";

[STU addobserver self forkeypath: @ "name" Options: nskeyvalueobservingoptionnew context: Nil ];

Stu. Name = @ ""; // call Method for value change

Stu. Name = @ ""; // call Method for value change

[STU release ];

Notification

1. Get notification center objects

Nsicationicationcenter * center = [nsnotificationcenter defacenter center]; // the actual address of the single instance is the address of the notification center.

2. Listening for notifications

[Center addobserver: Listener selector: method to be executed name: name of notification to the listener object: Notification sender];

3. The notification center publishes messages.

[Center postnotificationname: @ "Long live the King" Object: Someone];

4. Remove the listener Center

[Center removeobserver: Self name: @ "Long live the King" Object: Someone];

Demo:

King * King = [[King alloc] init];

Farmer * farmer = [[Farmer alloc] init];

Nsicationicationcenter * center = [nsnotificationcenter defacenter center];

[Center addobserver: farmer selector: @ selector (test :) name: @ "Long live the King" Object: King];

[Center postnotificationname: @ "Long live the King" Object: King];

[Center removeobserver: Farmer];

[King release];

[Farmer release];

-(Void) test :( nsnotification *) N // The method must implement what you want to do in the listener notification class.

{

Nslog (@ "Name: % @ object: % @ userinfo: % @", [nname], [n object], [n userinfo]);

}

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.