-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewController.m
39 lines (30 loc) · 1.09 KB
/
ViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//
// ViewController.m
// KVO原理探究
//
// Created by 颜仁浩 on 2018/3/23.
// Copyright © 2018年 颜仁浩. All rights reserved.
//
#import "ViewController.h"
#import "Student.h"
#import "NSObject+AJieKVO.h"
@interface ViewController ()
@property(nonatomic, strong)Student *student;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = UIColor.whiteColor;
Student *stu = [[Student alloc] init];
[stu jie_addObserver:self forKeyPath:@"stu_name" options:0 context:@"Student"];
_student = stu;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
self.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255) / 255.0 green:arc4random_uniform(255) / 255.0 blue:arc4random_uniform(255) / 255.0 alpha:1];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
static int i = 0;
NSString *name = [NSString stringWithFormat:@"stu_name%d", i];
_student.stu_name = name;
}
@end