2016-08-02 2 views
0

웹 서비스 데이터에 따라 동적으로 구성 요소를 표시하는 응용 프로그램을 만드는 중입니다. 하나 이상의 텍스트 필드가있을 때 올바르게 유효성 검사를 수행 할 수 없습니다. 첫 번째 텍스트 필드에 뭔가를 입력 한 후 두 번째 텍스트 필드를 클릭하면 유효성 검사를 위해 textFieldDidEndEditing에 작성한 코드가 textfield1에 대해 실행되지 않습니다.객관적인 c에있는 여러 텍스트 필드에서 유효성 검사를 수행하는 방법

for(int i=0;i<[textfields count];i++) 
{ 
_barcodeTextfield = [UITextField alloc] init]; 
_barcodeTextfield.delegate = self; 
[QuestionView addSubView:_barcodeTextField]; 
} 

및 검증 방법은, 검증이 실패하는 경우 경고를 표시 textfieldDidEndEditing 방법에 기록되어

은이 같은 루프에서 텍스트 필드에 대해 동일한 변수를 사용하고 있습니다. 내가 사전에 어떤 도움을 주셔서 대단히 두 번째 one.Thank로 커서를 이동할 때

if(textField== _barcodeTextField) 
{ 
//do the validation 
} 

처럼 그러나 이것은 최초의 텍스트 필드에 대해 실행되지 않습니다.

답변

0

for 루프에서 UITextField에 태그를 추가하십시오.

for(int i=0;i<[textfields count];i++) { 
    _barcodeTextfield = [UITextField alloc] init]; 
    _barcodeTextfield.delegate = self; 
    _barcodeTextfield.tag = i; 
    [QuestionView addSubView:_barcodeTextField]; 
} 

당신은이 PerformValidation 이런 식으로 뭔가 될 것 뷰 컨트롤러

- (void)textFieldDidEndEditing:(UITextField *)textField { 

    if (textField.tag == 1) { 
     // Store the first name in a property, or array, or something. 
    } Continue the same for other textfields 

    //Perform Validation 
    [self performValidation]; 
    } 

에 텍스트 필드의 대리자를 설정하여 입력 후 즉시 텍스트를 수집해야한다. 때문에 나는 (의 UITextField의 서브 클래스) 클래스 JSInputField을 상자에 포장 한 텍스트 필드에 유효성 검사를 추가하는 이러한 복잡성의

-(void)performValidation { 

if (firstProperty.length == 0) { 
    //Display alert 
} 
} 
0

. 한 줄의 코드로 텍스트 필드에 유효성 검사를 추가 할 수 있습니다.

JSInputField *inputField = [[JSInputField alloc] initWithFrame:CGRectMake(10, 100, 300, 50)]; 
[self.view addSubview:inputField]; 
[inputField setPlaceholder:@"Input Text"]; 
[inputField setRoundedCorners:UIRectCornerAllCorners]; 
[inputField addValidationRule:JSCreateRuleNotNullValue]; //This will validate field for null value. It will show error if field is empty. 
[inputField addValidationRule:JSCreateRuleLength(10)]; //This will validate field for string of length equal to or less than 10. 


JSInputField *inputField = [[JSInputField alloc] initWithFrame:CGRectMake(10, 100, 300, 50)]; 
[self.view addSubview:inputField]; 
[inputField setPlaceholder:@"Input Number"]; 
[inputField setRoundedCorners:UIRectCornerAllCorners]; 
[inputField addValidationRule:JSCreateRuleNotNullValue]; //This will validate field for null value. It will show error if field is empty. 
[inputField addValidationRule:JSCreateRuleNumeric(0)]; //This will validate field for numeric values and restrict to enter value upto 0 decimal places.