IOS--KVC && KVO

Source: Internet
Author: User

#import"ViewController.h"#import"Person.h"#import"Student.h"@interface Viewcontroller () {NSString*_text;} @property (Weak, nonatomic) Iboutlet Uitextfield*Plicceholder; @property (nonatomic, strong) person*Person ; @property (nonatomic, strong) Nsmutablearray*Temparray, @end @implementation viewcontroller//KVC key-value-coding//KVO, Key-value-observer, key-value observations, properties are triggered whenever the set method is called Kco- (void) viewdidload {[Super viewdidload]; [Self.plicceholder Setvalue:[uicolor Redcolor] Forkeypath:@"_placeholderlabel.textcolor"]; //property initialization, the call must be made using self.Self.person =[[Person alloc] init]; //1. Add the viewer//2.self.person is the observed object itself//Self , observer//KeyPath, observed instance variables//options to see what aspect[Self.person addobserver:self Forkeypath:@"name"Options:nskeyvalueobservingoptionnew Context:nil]; [Self.person addobserver:self Forkeypath:@"_age"Options:nskeyvalueobservingoptionnew Context:nil]; [Self addobserver:self forkeypath:@"_text"Options:nskeyvalueobservingoptionnew Context:nil]; Self.temparray=[@[] mutablecopy]; [Self addobserver:self forkeypath:@"Temparray"options:nskeyvalueobservingoptionnew Context:nil];}-(ibaction) Kvo_fire: (ID) Sender {//self.person.name = @ "Zhang vast";[Self.person SetValue:@" -"Forkey:@"_age"]; }//receive KVO Trigger event- (void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID)ObjectChange: (nsdictionary *) Change context: (void*) context{NSLog (@"Object =%@,keypath =%@",Object, KeyPath); //NSLog (@ "%@", keypath);}-(ibaction) Kvc_setvalueforkey: (ID) sender { person*p =[[Person alloc] init]; [P SetValue:@"Hauhau"Forkey:@"name"]; NSLog (@"%@", p.name); [P SetValue:@" -"Forkey:@"_age"]; NSLog (@"%@", [P Valueforkey:@"_age"]);}-(ibaction) Kvc_setvalueforpath: (ID) sender {Student*stu =[[Student alloc] init];//Person *p = [[Person alloc] init];//stu.per = p;[Stu SetValue:@"Hauhau"Forkeypath:@"Per.age"]; NSLog (@"%@", [Stu Valueforkeypath:@"Per.age"]); }-(ibaction) Kvc_setvaluserforkeyswithdictionary: (ID) sender {nsdictionary*dic = @{@"name":@"Zhanghauhao",@"Gender":@"male"}; person*p =[[Person alloc] init];        [P setvaluesforkeyswithdictionary:dic]; NSLog (@"%@", p.name); }-(ibaction) Kvc_sum: (ID) sender {nsmutablearray*ary =[@[] mutablecopy];  for(inti =0; I < -; i++) {[ary addobject:[nsstring stringWithFormat:@"%d", I]]; } NSLog (@"%@", [ary Valueforkeypath:@"@sum. IntegerValue"]); }-(ibaction) Kvo_observerself: (ID) Sender {[Self setValue:@"Biubiu"Forkey:@"_text"]; }- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}- (void) dealloc{//when you look at unwanted time, you need to write off, not absolutely in dealloc.[Self.person removeobserver:self Forkeypath:@"name"]; [Self.person removeobserver:self Forkeypath:@"_age"]; [Self removeobserver:self forkeypath:@"_text"];}-(ibaction) Kvo_mutablearray: (ID) Sender {//KVO Observation Arrays[Self Mutablearrayvalueforkey:@"Temparray"] AddObject:@"Zhaoahao"]; } @end
Person.h
#import <Foundation/Foundation.h>@interface person:nsobject{    **name; @end

Person.m

#import " Person.h " @implementation  Person // Attribute Association instance variable @synthesize name = _name; -(void) SetValue: (ID) value forundefinedkey: (NSString *) key{    }@end 

Student.h

#import <Foundation/Foundation.h>@class person ; @interface  *per; @end

Student.m

#import " Student.h " #import " Person.h " @implementation Student-(ID) init{    = [Super init];     if (self) {        = [[Person alloc] init];    }     return Self ;} @end

IOS--KVC && 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.