Self.model=[[datamodel Alloc]init];
[Self.model setvalue:@ "Searph" forkey:@ "StockName"];
[Self.model setvalue:@ "10.0" forkey:@ "Price"];
[Self.model addobserver:self forkeypath:@ "Price" options:nskeyvalueobservingoptionnew| Nskeyvalueobservingoptionold Context:nil];
Self.mylabel=[[uilabel alloc]initwithframe:cgrectmake (100, 100, 100, 30)];
Self.mylabel.layer.maskstobounds=yes;
self.mylabel.layer.cornerradius=50;
Self.mylabel.textcolor=[uicolor Cyancolor];
Self.mylabel.text=[self.model valueforkey:@ "Price"];
[Self.view AddSubview:self.myLabel];
Self.mybutton=[uibutton Buttonwithtype:uibuttontyperoundedrect];
Self.mybutton.frame=cgrectmake (200,200, 100, 30);
[Self.mybutton addtarget:self Action: @selector (ChangeValue) forcontrolevents:uicontroleventtouchupinside];
[Self.mybutton Setbackgroundcolor:[uicolor Lightgraycolor];
[Self.view AddSubview:self.myButton];
}
-(void) changevalue{
[Self.model setvalue:@ "20.0" forkey:@ "price"];
}
-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (nsdictionary *) Change context: (void *) context{
if ([KeyPath isequaltostring:@ "Price"]) {
Self.mylabel.text=[self.model valueforkey:@ "Price"];
}
}
Error when the program is running
Terminating app due to uncaught exception ' nsunknownkeyexception ', Reason: ' [<datamodel 0x7fe44bf22810> Setvalu E:forundefinedkey:]: This class was not key value coding-compliant for the key price. '
After inspection, Self.mylabel.text=[self.model valueforkey:@ "Price" was found, self.mylabel.text=[nsstring stringwithformat:@ "%@" was changed, [ Self.model valueforkey:@ "Price"];
Terminating app due to uncaught exception ' nsunknownkeyexception ', Reason: ' [<datamodel 0x7fe44b