marzapower/MPNumericTextField

Delegate pass-through

Closed this issue · 5 comments

I need to respond to textFieldDidBeginEditing, but setting the field's delegate breaks the functionality.

One possible solution: add

@property (nonatomic, weak) id<UITextFieldDelegate> passthroughDelegate;

in MPNumericTextField.h.

Then, in MPNumericTextFieldDelegate.m, change the method signatures to case to MPNumericTextField, and call the passthrough delegate.

For example:

- (BOOL)textFieldShouldReturn:(MPNumericTextField *)textField
{
  [textField resignFirstResponder];

  if ([textField.passthroughDelegate respondsToSelector:@selector(textFieldShouldReturn:)]) {
    return [textField.passthroughDelegate textFieldShouldReturn:textField];
  } else {
    return YES;
  }
}

All of the other methods would need to be implemented as well, for example:

- (void) textFieldDidBeginEditing:(MPNumericTextField *)textField {
  if ([textField.passthroughDelegate respondsToSelector:@selector(textFieldDidBeginEditing:)]) {
    [textField.passthroughDelegate textFieldDidBeginEditing:textField];
  }
}

Isn't the info found here (https://github.com/marzapower/MPNumericTextField#delegates) sufficient to fix this issue?

That fixes the issue, but requires pasting a bunch of duplicate code in multiple view controllers. (And with 0fa8552, requires passing back textFieldShouldClear: everywhere as well.)

(I apologize for not being clear about that in the issue description.)

That's perfectly fine. I understand your point, and was thinking about the same new issue with the new -textFieldShouldClear: delegate method called. I'll try to find a solution for both these problems.

Solved using 5dfdeca