經過 [object setValue : value forKey : key];對該對象的對應的實例變量進行賦值操做,省略了初試化步奏,使用得當能夠大大省略代碼量;
[object addObserver: observer forKeyPath:key options:options context:context];
- (void)observeValueForKeyPath:(NSString * )keyPath ofObject:( id )object change:(NSDictionary * )change context:(void * )context{};
1 A.設置將要監視的對象:
2
3 // StockData.h
4
5 #import <Foundation/Foundation.h>
6
7 @interface StockData : NSObject
8 {
9 NSString *stockName;
10 NSString *price;
11
12 }
13 @end
14 // StockData.m
15 #import "StockData.h"
16
17 @implementation StockData
18
19 @end
20
21
22 B、編輯主界面
23 // MainViewController.h
24 #import <UIKit/UIKit.h>
25
26 @interface MainViewController : UIViewController
27
28 @end
29
30 // MainViewController.m
31
32 #import "MainViewController.h"
33 #import "StockData.h"
34
35
36 @interface MainViewController ()
37
38 @property(nonatomic,retain)StockData *stockForKVO;
39
40 @property(nonatomic,retain)UILabel *aLabel;
41
42 @property(nonatomic,assign)NSInteger number;
43
44 @end
45
46 @implementation MainViewController
47
48 - (void)viewDidLoad {
49 [super viewDidLoad];
50 // Do any additional setup after loading the view.
51
52 self.number = 0;
53
54 _stockForKVO = [[StockData alloc]init];
55
56 [_stockForKVO setValue:@"searph" forKey:@"stockName"];
57
58 [_stockForKVO setValue:[NSString stringWithFormat:@"%ld",_number] forKey:@"price"];
59
60 [_stockForKVO addObserver:self forKeyPath:@"price" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
61
62 self.aLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 100, 30)];
63
64 self.aLabel.textAlignment = NSTextAlignmentCenter;
65 self.aLabel.textColor = [UIColor orangeColor];
66 self.aLabel.text = [_stockForKVO valueForKey:@"price"];
67 [self.view addSubview:self.aLabel];
68 // [self.aLabel release];
69
70 UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
71
72 btn.backgroundColor = [UIColor whiteColor];
73 btn.frame = CGRectMake(100, 30, 100, 30);
74
75 [btn addTarget:self action:@selector(handleDown:) forControlEvents:UIControlEventTouchUpInside];
76
77 [self.view addSubview:btn];
78
79 }
80
81 - (void)handleDown:(UIButton *)sender{
82
83
84
85
86
87 _number ++;
88
89 [_stockForKVO setValue:[NSString stringWithFormat:@"%ld",_number] forKey:@"price"];
90
91 }
92
93
94 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
95
96 if ([keyPath isEqualToString:@"price"]) {
97
98 self.aLabel.text = [_stockForKVO valueForKey:@"price"];
99
100
101 }
102
103
104
105
106 }
107
108
109 - (void)dealloc{
110 [super dealloc];
111 [_stockForKVO removeObserver:self forKeyPath:@"price"];
112 [_stockForKVO release];
113
114
115 }
116
117
118 - (void)didReceiveMemoryWarning {
119 [super didReceiveMemoryWarning];
120 // Dispose of any resources that can be recreated.
121 }
122
123 /*
124 #pragma mark - Navigation
125
126 // In a storyboard-based application, you will often want to do a little preparation before navigation
127 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
128 // Get the new view controller using [segue destinationViewController].
129 // Pass the selected object to the new view controller.
130 }
131 */
132
133 @end