2015-11-24 31 views
1

我已經添加文本框動態地如下滾動型: -設置最大長度,以動態地添加文本框

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.How我能做到這一點? 我作爲web服務的響應: -

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"; 
     } 
    ); 
+0

請參閱https://stackoverflow.com/questions/433337/set-the-maximum-character-length-of-a-uitextfield?rq=1。 – Daniel

回答

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

+0

響應因請求而異,所以我不能comapre文本字段佔位符,因爲在某些情況下,它可能有其他3個文本字段,因爲它是。 – poojathorat

+0

這樣你就可以用我的第二種方式完美運作 –

0

您也可以繼承UITextField並根據您的收款人響應將您關心的屬性直接添加到子類中作爲屬性。