Simple use of iOS KVC and KVO

Source: Internet
Author: User

Simple use of iOS KVC and KVO
# Import "ViewController. h"
# Import
@ Interface DataModel: NSObject
@ Property (nonatomic, copy) NSString * stockName;
@ Property (nonatomic, assign) float price;
@ End
@ Implementation DataModel
@ End

@ Interface ViewController ()
@ Property UILabel * myLabel;
@ Property UIButton * myButton;
@ Property DataModel * model;
@ End

@ Implementation ViewController

-(Void) viewDidLoad {
[Super viewDidLoad];

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 (130,100,100,100)];
Self. myLabel. layer. masksToBounds = YES;
Self. myLabel. layer. cornerRadius = 50;
Self. myLabel. textAlignment = NSTextAlignmentCenter;
Self. myLabel. textColor = [UIColor cyanColor];
Self. myLabel. backgroundColor = [UIColor grayColor];
Self. myLabel. text = [NSString stringWithFormat: @ "% @", [self. model valueForKey: @ "price"];
[Self. view addSubview: self. myLabel];

Self. myButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
Self. myButton. frame = CGRectMake (130,200,100, 30 );
[Self. myButton setTitle: @ "button" forState: UIControlStateNormal];
[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 isw.tostring: @ "price"]) {
Self. myLabel. text = [NSString stringWithFormat: @ "% @", [self. model valueForKey: @ "price"];
}
}
-(Void) dealloc {
[Self. model removeObserver: self forKeyPath: @ "price"];
}

-(Void) didReceiveMemoryWarning {
[Super didReceiveMemoryWarning];
}
@ 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.