将最大长度设置为动态添加的文本字段

时间:2015-11-24 05:32:30

标签: ios uitextfield uitextfielddelegate

我已将动态文本字段动态添加到scrollview,如下所示: -

for(int i = 0; i< responseBillPay.billerDetails.payeeFormat.count; i++) {
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, y, width, height)];
    textField.borderStyle = UITextBorderStyleRoundedRect;

    textField.placeholder = [responseBillPay.billerDetails.payeeFormat[i] name];
    textField.delegate = self;
  [innerScroll addSubview:textField];
}

现在我想为每个textfield设置最大长度。我怎么能这样做? 我得到了webservice的回复: -

payeeFormat =         
          (

         {

            displayFlag = false;
            length = 4;
            name = "BU(Billing Unit)";
            validation = "Please enter 4 digits BU(Billing Unit)";
        },
                    {
            displayFlag = false;
            length = 2;
            name = "PC(Processing Cycle)";
            validation = "Please enter only numbers in PC(Processing Cycle),PC(Processing Cycle) should be maximum 2 digits,PC(Processing Cycle) should be minimum 1 digit";
        },
                    {
            displayFlag = true;
            length = 12;
            name = "Consumer No.";
            validation = "Consumer No. should be maximum 12 digits,Consumer No. should be minimum";
        }
    );

2 个答案:

答案 0 :(得分:0)

根据你的说法,你可以让它变得有点动态。喜欢文本范围或占位符文本等

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString * searchStr = [textField.text stringByReplacingCharactersInRange:range withString:string];

    NSString *placeHOlder = textField.placeholder;

    if ([placeHOlder isEqualToString:@"BU(Billing Unit)"]) {
        if ([searchStr length] <= 4) {
            return YES;
        }
        else
            return NO;
    }
    else 
        return NO;
}

第二路

您可以将所有文本字段存储在数组中,并且可以在上面的委托方法中检查该文本字段。如果它相等,则输入您的文本范围并返回YES,否则返回NO

答案 1 :(得分:0)

您还可以将UITextField子类化,并根据收款人响应将您关心的属性直接添加到子类中作为属性。