5

I have a text field where I want to insert a phone number. The problem is that I use number pad and there is not any enter key and I without that key I don't know how to close the text field.

Is there any way of checking how many number os characters there are in a text field and close the number pad when there are X chars?

Sorry for my english and sorry for the noob question but I am just starting with ObjC.

Thanks *

Sorry guys, I forgot to tell you that I already have tried this:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    if (textField.text.length== 9) {
         [textField resignFirstResponder];

    }
    return YES;
}
Dyna
  • 2,304
  • 1
  • 14
  • 25
  • 1
    The reason this does not work is that you are checking the length before applying modifications. You need to grab the text, apply the change, and check the length of the result. See [this answer](http://stackoverflow.com/a/14960341/335858) for details. – Sergey Kalinichenko Sep 13 '13 at 10:37

7 Answers7

4

Yes, you have two choices in my opinion, you can:

A. Check the number of chars returned in delegate method shouldChangeTextInRange and then if your limit is reached resign first responder (when you resign first responder the keyboard dismisses,

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ( textView.text.length + (text.length - range.length) == 10) //or whatever value you like
    {
        [textField resignFirstResponder];
    } 
}

Or B:

Override the touchesEnded method of your view and call resignFirstResponder, so that when the user touches the view outside the textField, the keyboard dismisses. - this is what I do in my app.

Like this:

Just add this method to your viewController, it will be called automatically when the touch in the view ends, in this method you simply resignFirstResponder and the keyboard will disappear.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [textField resignFirstResponder];
}

I think it's better to do this in touchesEnded than began, it 'feels' nicer to have the keyboard dismiss when you lift your finger from the view, rather than when to initially touch it.

Woodstock
  • 22,184
  • 15
  • 80
  • 118
3

I found that resignFirstResponder do not include the last char typed. I had to do it in this way,

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    DLog(@"Existing: %@, string: %@, range:(%lu, %lu)", textField.text, string, (unsigned long)range.location, (unsigned long)range.length);

        if (textField == field2.textField) {

            if ( textField.text.length + (string.length - range.length) == kMyNumberLength) // e.g. 10
            {
                textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
                [textField resignFirstResponder];
            }

            if (textField.text.length + string.length - range.length > kMyNumberLength) {
                return NO;
            } else {
                return YES;
            }
        }       
    return YES;
}
karim
  • 15,408
  • 7
  • 58
  • 96
2

Swift

when reaching the maximum number of characters, add the new char to the current text in the textfield and then dismiss the keyboard

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    let maxLength = 5
    let currentString: NSString = textField.text! as NSString
    let newString: NSString = currentString.replacingCharacters(in: range, with: string) as NSString

    if newString.length == maxLength {
        textField.text = textField.text! + string
        textField.resignFirstResponder()
    }

    return newString.length <= maxLength
}
Musa almatri
  • 5,596
  • 2
  • 34
  • 33
1

Every time a user adds a number, the delegate of your text field will be notified through this method:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSUInteger length = [[textField text] length] - range.length + string.length;
    if (length == ...) ...
}

You can set up your delegate in such a way as to watch the length of the modified content of your text field, and close the field when the expected length is reached.

However, this has a high potential to frustrate your users a lot: they would make a mistake entering the last character every now and then, and the field is going to close on them, not letting them correct the problem. A better approach is to dismiss the pad when users tap away from your text field, which keeps end-users in control of what is going on:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [textField resignFirstResponder];
}
Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

You can just put a toolbar with a Done on it, on the click on that button you can hide keyboard and toolbar as well.

-(IBAction)doneButtonClicked:(id)sender{
[yourTextField resignFirstResponder];
[toolBar setHidden:YES];
}

or you can use this custom tool

Hemant Singh Rathore
  • 2,153
  • 1
  • 24
  • 38
0

Had issues with some of the suggestions above not retaining the last character entered.

I found this worked well for me.

Method simply examines the length of the text as it is building up and then runs [textField resignFirstResponder] to dismiss.

All usual textField delegates will run for the textField at point field loses focus and keyboard is dismissed.

- (void)textFieldDidChangeSelection:(UITextField *)textField {
    
    NSLog(@"textField.text: %@", textField.text);

    if (textField.text.length == 4) // If 4 is your max-length
        [textField resignFirstResponder];
    
}
Carl Hine
  • 1,817
  • 18
  • 24
-1

Use this textfield delegate method to find out the length of textField.text

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
   int textLength = [textField.text length] + 1;
   return YES;
}
Anuj
  • 820
  • 4
  • 11