当用户在文本字段中输入前4位数值时,如何显示选择器视图

时间:2014-09-29 06:00:45

标签: ios iphone database validation

我有一个UiTextField被叫MobileNumber。和两个pickerView称为运算符和圆。当我在textfield中输入我的号码的前4位数时,它会显示运算符pickerview, 当我在textfield中输入前4位数值并显示pickerview

时如何调用它

3 个答案:

答案 0 :(得分:0)

您可以声明将在文本字段中的每次更改时调用的通知。

// Add a "textFieldDidChange" notification method to the text field control.
[textField addTarget:self 
              action:@selector(textFieldDidChange:) 
    forControlEvents:UIControlEventEditingChanged];

现在在这个方法textFieldDidChange:您可以访问您的文本字段并检查用户是否输入了4位数字,您可以显示选择器。

答案 1 :(得分:0)

使用shouldChangeCharactersInRange textField的delegate方法输入numeric character

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([string rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]].location != NSNotFound)
    {
        //This field accepts only numeric entries.
        return NO;
    }
    else //numeric value entered
    {
        NSString *strTxtField = [textField.text stringByAppendingString:string];
        if(strTxtField.length == 4)
        {
            //show pickerview here
            //Set return NO if you don't want more character to be added to textfield
            //return NO;
        }
        return YES;
    }
}

答案 2 :(得分:0)

试试这段代码......

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    if(newString.length == 4)
    {
        NSLog(@"show date picker");
       //Set return NO if you don't want more character to be added to textfield
        //return NO;
    }

    return newString.length <= 4;
}

确保您的文本字段委托设置正确..