1.代替代理
/* 1.代替代理 */
self.textField.delegate = self;
[[self rac_signalForSelector:@selector(textFieldDidBeginEditing:)fromProtocol:@protocol(UITextFieldDelegate)] subscribeNext:^(RACTuple * _Nullable x) {
RACTupleUnpack(UITextField *textField) = x;
NSLog(@"textField:%@",textField);
}];
2.监听事件
/** 2.监听事件 */
[[self.button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIButton * _Nullable x) {
self.textField.text = @"88888888888";
}];
3.代替通知
/** 3.代替通知 */
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"Mr_zhang's" object:nil] subscribeNext:^(NSNotification * _Nullable x) {
NSLog(@"----%@----",x.name);
NSDictionary *dict = x.object;
NSLog(@"%@",dict);
}];
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"Mr_zhang",@"name",@"26",@"age",nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"Mr_zhang's" object:dict];
4.监听文本框改变
/** 4.监听文本框改变 */
[[self.textField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
NSLog(@"%@",x);
}];
5.代替KVO
/** 4.代替KVO */
[self.textField rac_observeKeyPath:@"text" options:NSKeyValueObservingOptionOld |NSKeyValueObservingOptionNew observer:nil block:^(id value, NSDictionary *change, BOOL causedByDealloc, BOOL affectedOnlyLastComponent) {
//回调
NSLog(@"value%@---%@",value,change);
}];
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
self.textField.text = @"222";
}
总结:使用RAC这些方法,可以让我们监听的事件或者使用的方法,都可以在Block回调到,代码可读性也大大提高