[iOS Learning Notes] KVO

Source: Internet
Author: User

//Properties@property (nonatomic) BOOL isfinished;
//Register for monitoring[Self addobserver:self Forkeypath:@"isfinished"Options0Context:null];
//Responding to change events- (void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID)ObjectChange: (nsdictionary *) Change context: (void*) context{if([KeyPath isequaltostring:@"isfinished"]) {NSLog (@"changeeddd"); } Else{[Super Observevalueforkeypath:keypath Ofobject:ObjectChange:change Context:context]; }}
//changing attribute values[button addtarget:self action: @selector (changefinished:) forcontrolevents:uicontroleventtouchupinside];-(void) Changefinished: (ID) Sender {//Automatic Notification[Self setvalue:[nsnumber numberwithbool:yes] Forkey:@"isfinished"];}

Register Listener Parameters Options

Nskeyvalueobservingoptionnew indicates that the property has not changed? previous value;

Nskeyvalueobservingoptionold represents the value after the property has changed;?

To remove a viewer:

[Subject Removeobserver:observer forkeypath:@ "name"];

KVO equivalent to Java:

Account.setlistener (New MyListener () {

Inspector.method ();

});

However, it is not necessary to declare a MyListener interface as in Java, nor to create a new Setlisteber (MyListener Listener) in the account class, so it is more flexible, but cannot be easily assigned when changing properties!

[iOS Learning Notes] KVO

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.