IOS learning: KVO

Source: Internet
Author: User

 

1 # import <Foundation/Foundation. h> 2 3 // The twfxstudata class is used as the class in the model layer to manage data 4 @ interface twfxstudata: nsobject 5 {6 nsstring * Name; 7 nsinteger age; 8} 9 10 @ property (retain, nonatomic) nsstring * schoolname; 11 12 @ end13 14 15 16 17 # import "twfxstudata. H "18 19 @ implementation twfxstudata20 21 @ end

 

1 // 2 // http://www.189works.com/article-103444-1.html 3 // KVO (key value coding) is a cocoa protocol used to set values or values (nskeyvaluecoding), a bit similar to Java EJB. 4 // both variables and function names are standardized to facilitate the setting of class member values. it is an important mechanism of cocoa, 5 // It is a bit similar to notification, but it provides a way to observe the changes of a certain attribute, and notification requires an object to send notification, in this way, KVO greatly simplifies the code than notification. 6 // This kind of observation-the observed model applies to such situations. For example, according to a property value change in a (data class), a property in B (View class) changes accordingly. For cocoa, which advocates MVC, KVO has a high application value. 7 // 8 9 # import "twfxviewcontroller. H "10 11 @ interface twfxviewcontroller () 12 13 @ end 14 15 @ implementation twfxviewcontroller 16 17-(void) viewdidload 18 {19 [Super viewdidload]; 20 // do any additional setup after loading the view, typically from a nib. 21 22 // twfxstudata is a data storage class used to simulate model data 23 studata = [[twfxstudata alloc] init]; 24 testcount = 1; 25 26 27 // Step 1: register the observer 28 // For the observer studata, it does not know who is observing it 29 // parameter description: the first parameter observer indicates who the observer is 30 // The second parameter keypath specifies the attribute to be observed 31 // The third parameter option can choose to observe the key value changes, for example, the value before and after the change 32 // The fourth parameter context needs to transmit data 33 34 [studata addobserver: Self 35 forkeypath: @ "name" 36 options: nskeyvalueobservingoptionnew | nskeyvalueobservingoptionold 37 context: nil]; 38 39 [studata addobserver: Self 40 forkeypath: @ "Age" 41 options: Unknown | nskeyvalueobservingoptionold 42 context: Nil]; 43 44 [studata addobserver: Self forkeypath: @ "schoolname" Options: nskeyvalueobservingoptionnew | nskeyvalueobservingoptionold context: Nil]; 45} 46 48 // Step 2: The Observer implements the change method. When the registered attribute changes, this method will be called 49 // parameter ofobject: the object will be observed. change: Changes to the observed attributes, which are specified during registration. context: 50-(void) observevalueforkeypath :( nsstring *) keypath 51 ofobject :( ID) object 52 change :( nsdictionary *) Change 53 context :( void *) context 54 {55 if ([keypath isinclutostring: @ "name"]) 56 {57 self. outlet_name.text = [studata valueforkey: @ "name"]; 58} 59 else if ([keypath ispath tostring: @ "Age"]) 60 {61 // the property value obtained through valueforkey is also 62 self of the object type. outlet_age.text = [nsstring stringwithformat: @ "% @", [studata valueforkey: @ "Age"]; 63} 64 else if ([keypath isw.tostring: @ "schoolname"]) 65 {66 self. outlet_schoolname.text = [studata valueforkey: @ "schoolname"]; 67} 68} 69 70 71 // Step 3: simulate a change in the value of the observed attribute 72-(ibaction) btnclick_changename :( uibutton *) sender {73 74 [studata setvalue: [nsstring stringwithformat: @ "liming % d", testcount] forkey: @ "name"]; 75 testcount ++; 76 77 // change the property value: method 1 78 [studata setvalue: @ "" forkey: @ "schoolname"]; 79} 80 81-(ibaction) btnclick_changeage :( uibutton *) sender {82 83 // when setting the attribute value through setvalue, the value must be of the object type, and the system should convert the value type 84 [studata setvalue: [nsnumber numberwithinteger: testcount] forkey: @ "Age"]; 85 testcount ++; 86 87 // change the attribute value: method 2 (valid for property) 88 [studata setschoolname: @ ""]; 89} 90 91-(ibaction) btnclick_test :( uibutton *) sender {92 93 // change the attribute value: method 3 (valid for property) 94 studata. schoolname = @ "d "; 95} 96 97 98 99 // Step 4: Disable observation 100-(void) dealloc {101 102 103 // disable Observation [studata removeobserver: self forkeypath: @ "name" context: Nil]; 104 [studata removeobserver: Self forkeypath: @ "Age" context: Nil]; 105 [studata removeobserver: Self forkeypath: @ "schoolname" context: Nil]; 106 107 // release 108 [studata release]; 109 110 [_ outlet_temp release]; 111 [_ outlet_name release]; 112 [_ outlet_age release]; 113 114 [_ outlet_schoolname release]; 115 [Super dealloc]; 116} 117 118} 119 @ end

 

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.