UITextFieldDelegate textFieldShould返回ReactiveCocoa

时间:2014-10-16 15:38:15

标签: ios uitextfield reactive-cocoa uitextfielddelegate

我正在尝试使用ReactiveCocoa实现UITextFieldDelegate textFieldShouldReturn处理。不幸的是,当我订阅信号时会运行subscribeNext块。

使用委托的实现将是:

- (void)viewDidLoad
{
    ...
    self.myTextField.delegate = self;
}

...

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == self.myTextField) {
        NSLog(@"Let's go!");
    }

    return YES;
}

在ReactiveCocoa中,我以类似UITextView + RACSignalSupport的方式为UITextField添加了一个类别。

@implementation UITextField (RACKeyboardSupport)

static void RACUseDelegateProxy(UITextField *self)
{
    if (self.delegate == self.rac_delegateProxy) return;

    self.rac_delegateProxy.rac_proxiedDelegate = self.delegate;
    self.delegate = (id)self.rac_delegateProxy;
}

- (RACDelegateProxy *)rac_delegateProxy
{
    RACDelegateProxy *proxy = objc_getAssociatedObject(self, _cmd);
    if (proxy == nil) {
        proxy = [[RACDelegateProxy alloc] initWithProtocol:@protocol(UITextFieldDelegate)];
        objc_setAssociatedObject(self, _cmd, proxy, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }

    return proxy;
}

- (RACSignal *)rac_keyboardReturnSignal
{
    @weakify(self);
    RACSignal *signal = [[[[RACSignal
                            defer:^{
                                @strongify(self);
                                return [RACSignal return:RACTuplePack(self)];
                            }]
                           concat:[self.rac_delegateProxy signalForSelector:@selector(textFieldShouldReturn:)]]
                          takeUntil:self.rac_willDeallocSignal]
                         setNameWithFormat:@"%@ -rac_keyboardReturnSignal", [self rac_description]];

    RACUseDelegateProxy(self);

    return signal;
}

@end

即使从未按下Return键,也会执行subscribeNext块:

- (void)viewDidLoad
{
    ...
    [self.myTextField.rac_keyboardReturnSignal subscribeNext:^(id x) {
        Log(@"Let's go with RAC!");
    }];
}

我必须使用skip:1来避免这个问题:

- (void)viewDidLoad
{
    ...
    [[self.myTextField.rac_keyboardReturnSignal skip:1] subscribeNext:^(id x) {
        Log(@"Let's go with RAC!");
    }];
}

知道为什么会这样吗?

解决方案:

- (RACSignal *)rac_keyboardReturnSignal
{
    RACSignal *signal = [[[self.rac_delegateProxy
                           signalForSelector:@selector(textFieldShouldReturn:)]
                          takeUntil:self.rac_willDeallocSignal]
                         setNameWithFormat:@"%@ -rac_keyboardReturnSignal", [self rac_description]];

    RACUseDelegateProxy(self);

    return signal;
}

1 个答案:

答案 0 :(得分:1)

您正在返回一个信号,该信号会立即返回defer块中的值,然后concat在调用textFieldShouldReturn时将新值添加到流中。

UITextView+RACSignalSupport.m中的代码正在调用reduceEach,以便返回从UITextView实例中提取的字符串值。 defer仅用于在订阅时生成初始值。

基本上,我认为您根本不需要defer用于您的用例。