限制多个UITextField的长度

发布于 2024-11-10 16:11:41 字数 1289 浏览 7 评论 0原文

我有四个 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 UITextFields, 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灼疼热情 2024-11-17 16:11:41

如果品牌、数量、尺寸和价格是实例变量(或属性)并且连接到您的 UITextFields,您可以尝试以下操作:

define MAXLENGTH 5
define MAXQTY 3
define MAXSIZE 4
define MAXBRAND 10

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField == brand) {
        int lengtha = [brand.text length];
        NSLog(@"lenghta = %d",lengtha);
        if (lengtha >= MAXBRAND && ![string isEqualToString:@""]) {
            brand.text = [brand.text substringToIndex:MAXBRAND];
            return NO;
        }
        return YES;
    } else if (textField == qty) {
        int lengthb = [qty.text length];
        NSLog(@"lenghtb = %d",lengthb);
        if (lengthb >= MAXQTY && ![string isEqualToString:@""]) {
            qty.text = [qty.text substringToIndex:MAXQTY];
            return NO;
        }
        return YES;
    } else if (textField == size) {
        int lengthc = [size.text length];
        NSLog(@"lenghtc = %d",lengthc);
        if (lengthc >= MAXSIZE && ![string isEqualToString:@""]) {
            size.text = [size.text substringToIndex:MAXSIZE];
            return NO;
        }
        return YES;
    } else if (textField == price) {
        int lengthd = [price.text length];
        NSLog(@"lenghtd = %d",lengthd);
        if (lengthd >= MAXLENGTH && ![string isEqualToString:@""]) {
            price.text = [price.text substringToIndex:MAXLENGTH];
            return NO;
        }
        return YES;
    }
    return YES;
}

If brand, qty, size and price are instance variables (or properties) and are connected to your UITextFields you could try this:

define MAXLENGTH 5
define MAXQTY 3
define MAXSIZE 4
define MAXBRAND 10

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField == brand) {
        int lengtha = [brand.text length];
        NSLog(@"lenghta = %d",lengtha);
        if (lengtha >= MAXBRAND && ![string isEqualToString:@""]) {
            brand.text = [brand.text substringToIndex:MAXBRAND];
            return NO;
        }
        return YES;
    } else if (textField == qty) {
        int lengthb = [qty.text length];
        NSLog(@"lenghtb = %d",lengthb);
        if (lengthb >= MAXQTY && ![string isEqualToString:@""]) {
            qty.text = [qty.text substringToIndex:MAXQTY];
            return NO;
        }
        return YES;
    } else if (textField == size) {
        int lengthc = [size.text length];
        NSLog(@"lenghtc = %d",lengthc);
        if (lengthc >= MAXSIZE && ![string isEqualToString:@""]) {
            size.text = [size.text substringToIndex:MAXSIZE];
            return NO;
        }
        return YES;
    } else if (textField == price) {
        int lengthd = [price.text length];
        NSLog(@"lenghtd = %d",lengthd);
        if (lengthd >= MAXLENGTH && ![string isEqualToString:@""]) {
            price.text = [price.text substringToIndex:MAXLENGTH];
            return NO;
        }
        return YES;
    }
    return YES;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文