Search code examples
iosuitextfieldreactive-cocoauitextfielddelegate

UITextFieldDelegate textFieldShouldReturn with ReactiveCocoa


I am trying to implement UITextFieldDelegate textFieldShouldReturn handling with ReactiveCocoa. Unfortunately the subscribeNext block is run when I subscribe for the signal.

The implementation using delegation would be:

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

...

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

    return YES;
}

In ReactiveCocoa I have added a category for UITextField in a similar fashion like UITextView+RACSignalSupport.

@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

Here subscribeNext block is executed even if the Return Key was never pressed:

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

I have to use skip:1 to avoid that problem:

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

Any idea why this happens?

Solution:

- (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;
}

Solution

  • You are returning a signal that immediately returns a value in your defer block, then concat-ing new values onto the stream when textFieldShouldReturn is invoked.

    The code in UITextView+RACSignalSupport.m is calling reduceEach in order to return a string value that is extracted from the UITextView instance. The defer is used to merely have an initial value generated upon subscription.

    Basically, I don't think you want the defer at all for your use case.