如何在UITextField上触发方法

时间:2014-07-02 14:14:03

标签: ios objective-c uitextfield

我有5个UITextFields彼此相邻。每一个都应该捕获一个pin号的数字。这就是我将输入限制为1个字符并将光标移动到下一个文本字段的方式。

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        if (self.pin1.text.length == 1 && range.length == 0) [self.pin2 becomeFirstResponder];
        if (self.pin2.text.length == 1 && range.length == 0) [self.pin3 becomeFirstResponder];
        if (self.pin3.text.length == 1 && range.length == 0) [self.pin4 becomeFirstResponder];
        if (self.pin4.text.length == 1 && range.length == 0) [self.pin5 becomeFirstResponder];
        return YES;
    }

一旦用户输入最后一个文本字段(pin5)中的最后一位数字,我想添加一些功能来检查引脚。现在NSLog会这样做。我该怎么做?

如果我将这行代码添加到shouldChangeCharactersInRange的末尾。您必须单击两次数字才能捕获第5位数字。

if (self.pin5.text.length == 1 && range.length == 0) NSLog(@"Pin %@, %@, %@, %@, %@", self.pin1.text, self.pin2.text, self.pin3.text, self.pin4.text, self.pin5.text);

4 个答案:

答案 0 :(得分:0)

如何添加此最终声明?

if (self.pin5.text.length == 1 && range.length == 0) {
    NSLog(@"Hey there!");
}

您可能还希望在开始时禁用所有字段(第一个除外),以便您可以逐个启用它们。

答案 1 :(得分:0)

在return语句之前添加此行:

if (self.pin5 == textField) {
    self.pin5.text = string;
    [self.pin5 resignFirstResponder]; // You can hide the keyboard if you like
    NSLog(@"Last text field");
}

答案 2 :(得分:0)

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (string.length > 0 &&  textField.text.length < 1) {
        textField.text = string;
        if (textField == self.pin1) [self.pin2 becomeFirstResponder];
        else if (textField == self.pin2) [self.pin3 becomeFirstResponder];
        else if (textField == self.pin3) [self.pin4 becomeFirstResponder];
        else if (textField == self.pin4) [self.pin5 becomeFirstResponder];
        else if (textField == self.pin5) NSLog(@"Pin %@, %@, %@, %@, %@", self.pin1.text, self.pin2.text, self.pin3.text, self.pin4.text, self.pin5.text);
    }
    return (textField.text.length + string.length) <= 1;
}

答案 3 :(得分:0)

你可以试试这个。像这样设置文本字段。

[self.pin1 addTarget:self 
              action:@selector(textFieldDidChange:) 
    forControlEvents:UIControlEventEditingChanged];
...
[self.pin5 addTarget:self 
              action:@selector(textFieldDidChange:) 
    forControlEvents:UIControlEventEditingChanged];

然后在另一种方法中执行以下操作:

-(void)textFieldDidChange:(id)sender {
    if (sender == self.pin1) [self.pin2 becomeFirstResponder];
        else if (sender == self.pin2) [self.pin3 becomeFirstResponder];
        else if (sender == self.pin3) [self.pin4 becomeFirstResponder];
        else if (sender == self.pin4) [self.pin5 becomeFirstResponder];
        else if (sender== self.pin5) {

            [self.pin5 resignFirstResponder];
            NSString *pinString = [NSString stringWithFormat:@"%@ %@ %@ %@ %@", self.pin1.text, self.pin2.text, self.pin3.text, self.pin4.text, self.pin5.text];
            NSLog(@"%@", pinString);
        }
}