ReactiveCocoa 常见使用

1.代替 tapGesture

 UITapGestureRecognizer *tap = [UITapGestureRecognizer new];
     [self.redView addGestureRecognizer:tap];
     [tap.rac_gestureSignal subscribeNext:^(id x) {
         NSLog(@"点击了view");
     }];

2.代替addTarget事件

// 按钮点击事件
[[self.signInButton  
   rac_signalForControlEvents:UIControlEventTouchUpInside]  
   subscribeNext:^(id x) {  
     NSLog(@"button clicked");  
   }]; 
// 文本框文字改变
    [[self.carPriceTF rac_signalForControlEvents:UIControlEventEditingChanged ] subscribeNext:^(id x) {
        UITextField *textField = x;
        if (textField.text.length > 4) {
            textField.text = [textField.text substringToIndex:4];
        }
    }];
// cell上按钮点击,防止因为循环利用,造成多次触发
 [[[cell.cancelButton rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:cell.rac_prepareForReuseSignal] subscribeNext:^(id x) {
        //
        [self requestToCancelExchangeAwardWithId:prizeModel.ex_id];
    }];
    return cell;

3 .代替观察者KVO

@weakify(self);
[RACObserve(self, value) subscribeNext:^(NSString* x) {
    @strongify(self);
    NSLog(@"XXX");
}];

4.监听 通知

    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillChangeFrameNotification object:nil]
        subscribeNext:^(id x) {
            NSLog(@"notificationDemo : %@", x);
        }];

5.代替代理

- (void)viewDidAppear:(BOOL)animated {
    self.chatContenTF.delegate = self;
    //
    [[self rac_signalForSelector:@selector(textFieldDidBeginEditing:) fromProtocol:@protocol(UITextFieldDelegate)] subscribeNext:^(RACTuple *tuple) {
        
    }];
    //
    [[self rac_signalForSelector:@selector(textFieldDidEndEditing:) fromProtocol:@protocol(UITextFieldDelegate)] subscribeNext:^(id x) {
        QYLog(@"%@",x);
    }];
}

你可能感兴趣的:(ReactiveCocoa 常见使用)