UIView를 만들고 싶습니다. 사용자가 편집을 마쳤을 때 여러 UITextFields가 각각의 유효성을 검사합니다. 뷰 컨트롤러는 각 UITextField에 대한 대리자입니다. 사용자가 해당 UITextField 중 하나의 값을 변경하고 키보드의 "완료"를 터치하거나보기의 다른 텍스트 필드를 터치하면 변경 내용을 저장하고 유효성을 검사합니다. 여기서 아이디어는 사용자에게 즉각적인 피드백을 제공하고 유효하지 않은 속성 값이 입력되면 더 이상 진행하지 못하게하는 것입니다. 입력 문자열의 유효성을 확인하는여러 UITextFields 유효성 검사
가장 위임 방법 textFieldShouldEndEditing됩니다 : 텍스트 필드와 textViewShouldEndEditing에 대한
나는
textFieldShouldEndEditing:
에 저장/유효성 검사 논리를 넣을 것을 제안 애플의 지원 문서,에 Text and Web Programming Guide 읽고 : 텍스트보기 용. 이 메소드는 텍스트 필드 또는 텍스트보기가 첫 번째 응답자 상태를 종료하기 직전에 호출됩니다. NO를 반환하면 그 일이 일어나지 않으며 결과적으로 텍스트 개체는 편집의 초점으로 유지됩니다. 입력 된 문자열이 유효하지 않은 경우 사용자에게 오류를 알리는 경고를 표시해야합니다.
그래서 이것을 테스트하기 위해 하나의 UIView와 두 개의 UITextField가있는 간단한 프로젝트를 만들었습니다. 문서별로,이 테스트 프로젝트에서 내가하는 것은 UIAlertView를 표시하고 NO를 반환하는 것입니다. 여기 방법 :
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
// return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
NSLog(@"In function: textFieldShouldEndEditing:(UITextField *)textField (tag=%i)", textField.tag);
[self logFirstResponder];
// PRETEND THAT THERE IS AN ISSUE THAT FAILS VALIDATION AND DISPLAY
// A UIALERTVIEW.
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Uh Oh!",@"")
message:@"This is a test error"
delegate:self
cancelButtonTitle:NSLocalizedString(@"OK",@"")
otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
NSLog(@"Displaying Error UIAlertView!!!");
// SINCE THE VALIDATION FAILED, RETURN NO TO HOLD THE USER IN THE
// UITEXTFIELD.
return NO;
}
그리고 여기에 문제가있다 : 다른 위에 하나의 UITextField에서 사용자가 클릭,이 방법은 3 회를 호출하면 그 결과로 UIAlertView는 3 회 표시됩니다. 다음은 내 테스트의 콘솔 로그입니다.
-- Field One tag = 100, Field Two tag = 200 --
2010-07-02 09:52:57.971 test project[22866:207] In function: textFieldShouldBeginEditing:(UITextField *)textField (tag=100)
2010-07-02 09:52:57.977 test project[22866:207] In function: textFieldDidBeginEditing:(UITextField *)textField (tag=100)
2010-07-02 09:52:57.977 test project[22866:207] Field One is the First Responder.
-- now i'm going to click from Field One into Field Two --
2010-07-02 09:53:18.771 test project[22866:207] In function: textFieldShouldBeginEditing:(UITextField *)textField (tag=200)
2010-07-02 09:53:18.772 test project[22866:207] Field One is the First Responder.
2010-07-02 09:53:18.774 test project[22866:207] In function: textFieldShouldEndEditing:(UITextField *)textField (tag=100)
2010-07-02 09:53:18.774 test project[22866:207] Field One is the First Responder.
2010-07-02 09:53:18.778 test project[22866:207] Displaying Error UIAlertView!!!
2010-07-02 09:53:18.780 test project[22866:207] In function: textFieldShouldBeginEditing:(UITextField *)textField (tag=200)
2010-07-02 09:53:18.781 test project[22866:207] Field One is the First Responder.
2010-07-02 09:53:18.781 test project[22866:207] In function: textFieldShouldEndEditing:(UITextField *)textField (tag=100)
2010-07-02 09:53:18.782 test project[22866:207] Field One is the First Responder.
2010-07-02 09:53:18.783 test project[22866:207] Displaying Error UIAlertView!!!
그래서 거래가 무엇입니까? 그것은 뭔가를 놓친 것 같습니다 ... 어떻게 UITextField 유효성을 검사하고 오류를 제대로 표시합니까?