Cannot hide keyboard with shouldChangeCharactersInRange

This is my code:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{
    NSCharacterSet *nonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:@"אבגדהוזחטיכלמנסעפצקרשתףץםן"] invertedSet];

    // max charcters
    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    if (newLength > 14)
        return NO;

    // allow backspace
    if (range.length > 0 && [string length] == 0) {
        return YES;
    }
    // do not allow . at the beggining
    if (range.location == 0 && [string isEqualToString:@"."]) {
        return NO;
    }
    if ((range.location > 0) && (([[textField.text substringWithRange:NSMakeRange((range.location -1), 1)] isEqualToString:@"ף"]) || ([[textField.text substringWithRange:NSMakeRange((range.location -1), 1)] isEqualToString:@"ץ"]) || ([[textField.text substringWithRange:NSMakeRange((range.location -1), 1)] isEqualToString:@"ם"]) || ([[textField.text substringWithRange:NSMakeRange((range.location -1), 1)] isEqualToString:@"ן"])))   
    {
        return NO;
    }

    // set the text field value manually
    NSString *newValue = [[textField text] stringByReplacingCharactersInRange:range withString:string];
    newValue = [[newValue componentsSeparatedByCharactersInSet:nonNumberSet] componentsJoinedByString:@""];
    textField.text = newValue;
    // return NO because we're manually setting the value
    return NO;
}

I just want to do, when you press Return, the keyboard will disappear. I can not do it. where to add it and how?

+3
source share
3 answers

If you want to hide the keyboard after pressing a key return, add the following to your code:

if ([string isEqualToString:@"\n"]) {
    [theTextField resignFirstResponder];
    return NO;
}

Hope this helps

+7
source

in the delegate method of UITextField textFieldShouldReturn use: -

[yourTextField resignFirestResponder];

Also implement a UITextFieldDelegate in your class and set the delegate to viewDidLoad as: -

yourTextField.delegate=self;
+3
source

IBAction TextField.

-(IBAction)doneEditing:(id)sender
{
     [sender resignFirstResponder];
}

. .

+1

All Articles