限制多个UITextField的长度
我有四个 UITextField,并且想限制每个的长度,但它们的长度都不同。到目前为止我有以下代码,但它不能正常工作
define MAXLENGTH 5
define MAXQTY 3
define MAXSIZE 4
define MAXBRAND 10
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
int lengtha = [brand.text length] ;
NSLog(@"lenghta = %d",lengtha);
if (lengtha >= MAXBRAND && ![string isEqualToString:@""]) {
brand.text = [brand.text substringToIndex:MAXBRAND];
return NO;
}
return YES;
int lengthb = [qty.text length] ;
NSLog(@"lenghtb = %d",lengtha);
if (lengthb >= MAXQTY && ![string isEqualToString:@""]) {
qty.text = [qty.text substringToIndex:MAXQTY];
return NO;
}
return YES;
int lengthc = [size.text length] ;
NSLog(@"lenghtc = %d",lengtha);
if (lengthc >= MAXSIZE && ![string isEqualToString:@""]) {
size.text = [size.text substringToIndex:MAXSIZE];
return NO;
}
return YES;
int lengthd = [price.text length] ;
NSLog(@"lenghtd = %d",lengtha);
if (lengthd >= MAXLENGTH && ![string isEqualToString:@""]) {
price.text = [price.text substringToIndex:MAXLENGTH];
return NO;
}
return YES;
}
I have four UITextField
s, and would like to limit the length of each one, but they are all different lengths. I have the following code so far, but it doesn't quite work correctly
define MAXLENGTH 5
define MAXQTY 3
define MAXSIZE 4
define MAXBRAND 10
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
int lengtha = [brand.text length] ;
NSLog(@"lenghta = %d",lengtha);
if (lengtha >= MAXBRAND && ![string isEqualToString:@""]) {
brand.text = [brand.text substringToIndex:MAXBRAND];
return NO;
}
return YES;
int lengthb = [qty.text length] ;
NSLog(@"lenghtb = %d",lengtha);
if (lengthb >= MAXQTY && ![string isEqualToString:@""]) {
qty.text = [qty.text substringToIndex:MAXQTY];
return NO;
}
return YES;
int lengthc = [size.text length] ;
NSLog(@"lenghtc = %d",lengtha);
if (lengthc >= MAXSIZE && ![string isEqualToString:@""]) {
size.text = [size.text substringToIndex:MAXSIZE];
return NO;
}
return YES;
int lengthd = [price.text length] ;
NSLog(@"lenghtd = %d",lengtha);
if (lengthd >= MAXLENGTH && ![string isEqualToString:@""]) {
price.text = [price.text substringToIndex:MAXLENGTH];
return NO;
}
return YES;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果品牌、数量、尺寸和价格是实例变量(或属性)并且连接到您的 UITextFields,您可以尝试以下操作:
If brand, qty, size and price are instance variables (or properties) and are connected to your UITextFields you could try this: