3

这是我的代码:

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

我只是想当你点击Return键盘时会消失。我不能那样做。在哪里添加它以及如何添加?

4

3 回答 3

7

如果您想在点击return键后隐藏键盘,请将以下代码添加到您的代码中:

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

希望能帮助到你

于 2012-05-24T05:44:38.293 回答
3

在 UITextField textFieldShouldReturn 的委托方法中使用:-

[yourTextField resignFirestResponder];

还要在您的类中实现 UITextFieldDelegate 并将 viewDidLoad 中的委托设置为:-

yourTextField.delegate=self;
于 2012-05-24T05:43:41.993 回答
1

创建一个 IBAction 并将其与您的 TextField 视图连接起来。

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

在连接到文件所有者时选择事件在退出时结束。

于 2012-05-24T06:18:13.860 回答