RAC的一些用法
1.代替KVO
- 当self.view的Frame发生变化是下边两个方法都能调用内部的Block
[self.view rac_observeKeyPath:@"frame" options:NSKeyValueObservingOptionNew observer:nil block:^(id value, NSDictionary *change, BOOL causedByDealloc, BOOL affectedOnlyLastComponent) {
}];
[[self.view rac_valuesForKeyPath:@"frame" observer:nil] subscribeNext:^(id x) {
}];
2.监听事件
[[_btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
NSLog(@"按钮点击事件");
}];
3.代替通知
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillShowNotification object:nil] subscribeNext:^(id x) {
NSLog(@"键盘弹出");
}];
4.监听文本框的改变
[_textField.rac_textSignal subscribeNext:^(id x) {
NSLog(@"%@", x);
}];
5.代替代理
// 1.代替代理:1.RACSubject 2.rac_signalForSelector
// 只要传值,就必须使用RACSubject
[[_redView rac_signalForSelector:@selector(btnClick:)] subscribeNext:^(id x) {
NSLog(@"控制器知道按钮被点击");
}];