oc版pod:pod 'ReactiveObjC'
1.Button 点击方法
[[self.button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindofUIControl*_Nullablex) {
}];
2.监听TeXField 输入
[_userNameTF.rac_textSignal subscribeNext:^(NSString*_Nullablex) {
NSLog(@"textField.text == %@",x);
}];
[[_userNameTF.rac_textSignal filter:^BOOL(NSString *_Nullablevalue) {
returnvalue.length >=4;
}]
subscribeNext:^(NSString*_Nullablex) {
NSLog(@"%@",x);
}];
3.便利数组字典
NSArray *arr = @[@"1", @"2", @"3", @"4", @"5"];
[arr.rac_sequence.signal subscribeNext:^(id _Nullablex) {
NSLog(@"遍历数组 %@",x);
}];
NSDictionary *dic = @{@"name":@"yangBo", @"age":@"19"};
[dic.rac_sequence.signal subscribeNext:^(RACTuple *_Nullablex) {
NSLog(@"遍历字典 %@",x);
}]
4.通知 可省略此方法里的清除通知
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardDidShowNotification object:nil] subscribeNext:^(idx) {
NSLog(@"键盘弹起");
}];
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"postData"object:nil]subscribeNext:^(NSNotification * notification) {
NSLog(@"%@",notification.name);
NSLog(@"%@",notification.object);
}];
5.代理
// 监听点击了Cell里的button点击事件 button的点击方法是buttonAction:
[[cell rac_signalForSelector:@selector(buttonAction:)] subscribeNext:^(id _Nullablex) {
NSLog(@"点击了button%@",x);
}];
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"RAC"message:@"RAC TEST"delegate:selfcancelButtonTitle:@"CANCEL"otherButtonTitles:@"other",@"1111",@"2222",nil];
[[selfrac_signalForSelector:@selector(alertView:clickedButtonAtIndex:)fromProtocol:@protocol(UIAlertViewDelegate)]subscribeNext:^(RACTuple *tuple) {
NSLog(@"tuple%@",tuple);
}];
[[alertView rac_buttonClickedSignal]subscribeNext:^(idx) {
NSLog(@"%@",x);
}];
6.定时器
[[RACScheduler mainThreadScheduler]afterDelay:3schedule:^{
NSLog(@"3秒后执行一次");
}];
//每隔两秒执行一次
//这里要加takeUntil条件限制一下否则当控制器pop后依旧会执行
[[[RACSignal interval:2onScheduler:[RACScheduler mainThreadScheduler]] takeUntil:self.rac_willDeallocSignal ] subscribeNext:^(idx) {
NSLog(@"每两秒执行一次");
}]