我有5个UITextFields,其中只允许6个字符。如何将输入限制为每个文本字段仅6个字符?我试过的任何其他代码都失败了,我没有找到修复它的方法,谢谢:D 编辑:这是我测试的代码:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSUInteger newLength1 = [serie1.text length] + [string length] - range.length;
NSUInteger newLength2 = [serie2.text length] + [string length] - range.length;
NSUInteger newLength3 = [serie3.text length] + [string length] - range.length;
NSUInteger newLength4 = [serie4.text length] + [string length] - range.length;
NSUInteger newLength5 = [serie5.text length] + [string length] - range.length;
if(newLength1 > 6)
return NO;
if(newLength2 > 6)
return NO;
if(newLength3 > 6)
return NO;
if(newLength4 > 6)
return NO;
if(newLength5 > 6)
return NO;
return YES;
}
和
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(serie1.text.length >= 6 && range.length == 0) {
return NO;
}
if(serie2.text.length >= 6 && range.length == 0) {
return NO;
}
if(serie3.text.length >= 6 && range.length == 0) {
return NO;
}
if(serie4.text.length >= 6 && range.length == 0) {
return NO;
}
if(serie5.text.length >= 6 && range.length == 0) {
return NO;
}
return YES;
}
两者都有这样的问题:当你写6个字符时,你不能再写任何其他文本字段,直到你删除你写的字符
答案 0 :(得分:11)
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField.text.length >= 7 && range.length == 0)
{
return NO;
}
return YES;
}
答案 1 :(得分:0)
实施UITextField
委托方法:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSUInteger newLength = [textField.text length] + [string length] - range.length;
if(newLength > 6)
return NO;
return YES;
}