Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在输入四个字符后写入UItextfield长度和更改键盘数字格式的条件_Ios_Objective C_Uitextfield_Uitextfielddelegate_Uikeyboardtype - Fatal编程技术网

Ios 如何在输入四个字符后写入UItextfield长度和更改键盘数字格式的条件

Ios 如何在输入四个字符后写入UItextfield长度和更改键盘数字格式的条件,ios,objective-c,uitextfield,uitextfielddelegate,uikeyboardtype,Ios,Objective C,Uitextfield,Uitextfielddelegate,Uikeyboardtype,在Uitextfield中,长度为6,前四个字符必须是字符串,其余两个必须是数字,当我输入前四个字符时,我尝试输入第五个字符键盘更改为数字格式,那么我如何在目标c中写入上述条件。可以使用其属性键盘类型以编程方式更改键盘类型。由于您必须检查每个输入,您需要使用哪个键盘来实现shouldChangeCharactersInRange的委托方法UITextFieldDelegate - (BOOL)textField:(UITextField *)textField shouldChangeChara

在Uitextfield中,长度为6,前四个字符必须是字符串,其余两个必须是数字,当我输入前四个字符时,我尝试输入第五个字符键盘更改为数字格式,那么我如何在目标c中写入上述条件。

可以使用其属性
键盘类型以编程方式更改键盘类型。由于您必须检查每个输入,您需要使用哪个键盘来实现
shouldChangeCharactersInRange
的委托方法
UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{

   // NSLog(@"%d",range);
 NSString * r = [NSString stringWithFormat:@"%d", range];
 NSLog(@"%@",r);
 if ([r  isEqual: @"4"])
 {
    [_txtView setKeyboardType:UIKeyboardTypeNumberPad];
    [self.view endEditing:YES];
    [_txtView becomeFirstResponder];

 }


 return YES;
}
下面是一个示例,您可以对其进行优化以减少重新加载的次数

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textfield == <Your texfield>) { // this condition is required in case of multiple textfields in the same view else you can skip
        NSString *finalString = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if (finalString.length <= 6) {
            if (finalString.length > 3) {
                textField.keyboardType = UIKeyboardTypePhonePad;
            } else {
                textField.keyboardType = UIKeyboardTypeDefault;
            }
            [textField reloadInputViews];
        } else {
            return NO;
        }
    }
}
-(BOOL)textField:(UITextField*)textField应更改字符范围:(NSRange)范围替换字符串:(NSString*)字符串
{
如果(textfield==){//如果同一视图中有多个textfield,则需要此条件,否则可以跳过
NSString*finalString=[textField.text stringByReplacingCharactersInRange:range with string:string];
如果(最后一环长度3){
textField.keyboardType=UIKeyboardTypePhonePad;
}否则{
textField.keyboardType=UIKeyboardTypeDefault;
}
[文本字段重新加载输入视图];
}否则{
返回否;
}
}
}
func textField(\utextfield:UITextField,shouldChangeCharacters范围:NSRange,replacementString:string)->Bool

您可以使用此委托函数并根据需要添加所有必要的条件。下面我展示了一些示例通用代码。您还可以根据文本长度键入键盘

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{


    //length condition and also trim the string before yu check for space and dot(.)
    if(textField.text?.characters.count == 6){
        return false
    }

    //alphabetic condition
    if((textField.text?.characters.count <= 4)){

        if(string == "number"){
            return false
        }
    }else if((textField.text?.characters.count > 4  )){
        if(string == "alphabetic"  ){
            return false
        }

    }

    return true
}
func textField(textField:UITextField,shouldChangeCharacters范围:NSRange,replacementString:string)->Bool{
//长度条件,并在检查空格和圆点之前修剪字符串(.)
if(textField.text?.characters.count==6){
返回错误
}
//字母条件
如果((textField.text?.characters.count 4)){
如果(字符串==“字母”){
返回错误
}
}
返回真值
}
-(BOOL)textField:(UITextField*)textField应更改字符范围:(NSRange)范围替换字符串:(NSString*)字符串{
if(string.length==0){
如果(textField.text.length==4){
textField.keyboardType=UIKeyboardTypeDefault;
textField.resignFirstResponder;
textField.becomeFirstResponder;
}
}否则{
如果(textField.text.length==3){
textField.keyboardType=UIKeyboardTypeNumberPad;
textField.resignFirstResponder;
textField.becomeFirstResponder;
返回否;
}如果(textField.text.length<4){
NSCharacterSet*cs=[[NSCharacterSet characterSetWithCharactersInString:@“ABCDEFGHIJKLMNOPQRSTUVWXYZABCDFGHIJKLMNOPQRSTUVWXYZ”]逆变器集];
NSString*筛选=[[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@”“];
返回[字符串isEqualToString:已筛选];
}else if(textField.text.length==6){
返回YES;
}
}
返回true;
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (string.length == 0) {
    if (textField.text.length == 4) {
        textField.keyboardType = UIKeyboardTypeDefault;
        textField.resignFirstResponder;
        textField.becomeFirstResponder;
    }
} else {
    if (textField.text.length == 3) {
        textField.keyboardType = UIKeyboardTypeNumberPad;
        textField.resignFirstResponder;
        textField.becomeFirstResponder;
        return NO;
    } if (textField.text.length < 4) {
        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        return [string isEqualToString:filtered];
    } else if (textField.text.length == 6) {
        return YES;
    }
}

return true;