Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextFieldDelegates not getting called #16

Closed
ghost opened this issue May 5, 2016 · 5 comments
Closed

TextFieldDelegates not getting called #16

ghost opened this issue May 5, 2016 · 5 comments

Comments

@ghost
Copy link

ghost commented May 5, 2016

As I want to limit the text entering to a certain length, I was implementing the -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string method but somehow I observed it is not getting called for this textfield though the delegate is assigned.

The code for limiting the text to 10 digits is follows:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField == textFieldPhone) {
NSString *resultingString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSInteger length = [resultingString length];
if (length > 10)
return NO;
NSCharacterSet *characterSet = [[NSCharacterSet characterSetWithCharactersInString:ACCEPT_NUMERIC] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:characterSet] componentsJoinedByString:@""];
return [string isEqualToString:filtered];
}
return YES;
}

@matmartinez
Copy link
Owner

matmartinez commented May 5, 2016

Hi there rupammitra,

The text field won't call the -textField:shouldChangeCharactersInRange: method if you're using a custom -inputView. Instead, you should take a look at MMNumberKeyboardDelegate:

- (BOOL)numberKeyboard:(MMNumberKeyboard *)numberKeyboard shouldInsertText:(NSString *)text;

All you have to do is set the delegate of your number keyboard, like this:

MMNumberKeyboard *keyboard = [[MMNumberKeyboard alloc] initWithFrame:CGRectZero];
keyboard.delegate = self;

Hope this helps,

Matías.

@ghost
Copy link
Author

ghost commented May 5, 2016

Have used it. But still not worked.

@matmartinez
Copy link
Owner

matmartinez commented May 5, 2016

What do you mean by not worked? Can you be a little more specific?

There is a chance your implementation is wrong. Here's a example:

- (BOOL)numberKeyboard:(MMNumberKeyboard *)numberKeyboard shouldInsertText:(NSString *)text
{
      const NSInteger maximumLength = 10;

      return (self.yourTextField.length + text.length <= maximumLength);
}

Friendly reminder that this is not StackOverflow. Let me know if this solves your issue.

Matías.

@ghost
Copy link
Author

ghost commented May 6, 2016

Yes this worked. Thanks Matias.

@ghost ghost closed this as completed May 6, 2016
@matmartinez
Copy link
Owner

Great!

Matías.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant