2013-08-21 4 views
0

UIAlertView을 서브 클래 싱했으며 그 내부에 입력을 취하는 textField이 표시됩니다. 사용자가 textField을 클릭하면 키보드가 나타나고 UIAlertView은 위로 이동하여 키보드를 조정합니다. 그러나 [textField becomeFirstResponder]을 대의 대리자 방법으로 UIAlertView으로 지정하면 alertView가 위로 이동하지 않고 키보드를 조정합니다. 대신 UIAlertView은 키보드 뒤에 숨어 있습니다.UIAlertView가 위로 이동하지 않습니다. 키보드가 becomeFirstResponder에 나타날 때

PS - Apple이 UIAlertView을 서브 클래 싱해서 그대로 사용하면 안된다고 알고 있지만, UIAlertView을 서브 클래 싱하는 중입니다. 애플의 기본 UI 요소를 다시 디자인하고 싶기 때문입니다.

답변

1

정말 애플의 추천에 어긋나지 마라.

이유

  1. 당신은 당신이 직면하고있는 것과 같은 예상치 못한 문제에 직면 할 수 있습니다.
  2. 권장 사항을 위반하면 코드가 향후 출시 될 iPhone을 위해 중단 될 수 있습니다.
  3. Apple의 표준 컨트롤을 재 설계하는 것이 HIG 지침을 위반하는 것입니다. 결과적으로 앱이 거부 될 가능성이 있습니다. 대신 하위 클래스 UIView을 사용하여 나만의 것을 만드십시오.

대신 Apple은이 요구 사항에 대해 UIAlertView 조항을 마련했습니다. 경고보기에 텍스트 필드를 추가 할 필요없이 UIAlertView 속성 alertViewStyle을 사용하십시오. 그것은 열거 UIAlertViewStyle

typedef NS_ENUM(NSInteger, UIAlertViewStyle) { 
    UIAlertViewStyleDefault = 0, 
    UIAlertViewStyleSecureTextInput, // Secure text input 
    UIAlertViewStylePlainTextInput, // Plain text input 
    UIAlertViewStyleLoginAndPasswordInput // Two text fields, one for username and other for password 
}; 

예에 정의 된 값을 허용, 당신은 사용자의 비밀번호를 수용 할 유스 케이스를 가정 할 수 있습니다. 이를 달성하기위한 코드는 다음과 같습니다.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please enter password" 
                message:nil 
               delegate:self 
             cancelButtonTitle:@"Cancel" 
             otherButtonTitles:@"Continue", nil]; 
[alert setAlertViewStyle:UIAlertViewStyleSecureTextInput]; 
[alert show]; 

입력의 유효성을 검사하려면,

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView 
{ 
    NSString *inputText = [[alertView textFieldAtIndex:0] text]; 
    if([inputText length] >= 6) 
    { 
     return YES; 
    } 
    else 
    { 
     return NO; 
    } 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; 
    if([title isEqualToString:@"Login"]) 
    { 
     UITextField *password = [alertView textFieldAtIndex:0]; 
     NSLog(@"Password: %@", password.text); 
    } 
} 

에 다시 반복 처리 사용자 입력을 얻으려면, 암호 입력이 최소 6 자이 대리자 메서드를 구현해야합니다 말할 수 , UIAlertView에는 개인보기 계층 구조가 있으며 수정하지 않고 그대로 사용하는 것이 좋습니다. 추천에 대해 사용하면 예기치 않은 결과가 발생합니다. 있는 그대로와 하위 클래스를 지원하지 않습니다

Apple docs

에서 UIAlertView 클래스는 사용하기위한 것입니다. 이 클래스의보기 계층 구조는 비공개이며 수정할 수 없습니다.

이것은 iOS 기본 앱에서도 사용되는 표준 기술입니다 (예 : Wi-Fi 비밀번호 입력 등).) 따라서 이것을 사용하면 언급 한 것과 같은 문제에 직면하지 않도록 할 수 있습니다.

희망 하시겠습니까?

0

텍스트 파일을 표시하는 화면을 좋아합니다. :-)이 도움이 되었으면합니다.

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

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDelegate:self]; 
[UIView setAnimationDuration:0.5]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 
self.view.frame = CGRectMake((self.view.frame.origin.x), (self.view.frame.origin.y-50), self.view.frame.size.width, self.view.frame.size.height); 
[UIView commitAnimations]; 
} 


- (void) textFieldDidEndEditing:(UITextField *)textField { 
[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDelegate:self]; 
[UIView setAnimationDuration:0.5]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y+50 , self.view.frame.size.width, self.view.frame.size.height); 
[UIView commitAnimations]; 
} 
+0

제 질문은'becomeFirstResponder'를 호출 할 때'UIAlertView'가 자동으로 위로 움직여서는 안됩니다. 당신이 제안한 방법은 훌륭하고 분명히 그 일을 할 것이지만 나는 그렇게하지 말아야한다는 것을 궁금해하며,'UIAlertView'가 자동으로 그렇게 할 것입니다. – nefarianblack

관련 문제