我有一个UiTextField
被叫MobileNumber。和两个pickerView
称为运算符和圆。当我在textfield
中输入我的号码的前4位数时,它会显示运算符pickerview
,
当我在textfield
中输入前4位数值并显示pickerview
答案 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;
}
确保您的文本字段委托设置正确..