영삼이의 IT정보2012. 6. 20. 13:17

iOS 5부터 UIAlertView에 alertViewStyle 프라퍼티가 추가되었다. 기존에 약간의 커스텀 작업을 통해 텍스트필드를 추가할 수 있었는데 이제는 간단히 상수를 이용하여 원하는 작업을 좀더 간단히 할 수 있게 되었다. 자 간단히 살펴보자.

1. 기본 스타일

UIAlertView *alertView = [[UIAlertView allocinitWithTitle:@"기본 스타일"

                                                    message:@"기본 스타일의 얼럿 입니다."

                                                   delegate:self

                                          cancelButtonTitle:@"취소"

                                          otherButtonTitles:@"확인"nil];

[alertView show]

 

 
2. 텍스트 입력 스타일

UIAlertView *alertView = [[UIAlertView allocinitWithTitle:@"텍스트 입력 스타일"

                                                    message:@"텍스트 입력 스타일의 얼럿 입니다."

                                                   delegate:self

                                          cancelButtonTitle:@"취소"

                                          otherButtonTitles:@"확인"nil];

alertView.alertViewStyle = UIAlertViewStylePlainTextInput;

[alertView show];


이 경우 텍스트 필드에 입력된 데이터의 접근은 다음과 같이 한다.

UITextField *textField = [alertView textFieldAtIndex:0];

NSLog(@"Plain text input: %@",textField.text);


 3. 압호 입력 스타일

UIAlertView *alertView = [[UIAlertView allocinitWithTitle:@"암호 입력 스타일"

                                                    message:@"암호 입력 스타일의 얼럿 입니다."

                                                   delegate:self

                                          cancelButtonTitle:@"취소"

                                          otherButtonTitles:@"확인"nil];

alertView.alertViewStyle = UIAlertViewStyleSecureTextInput;

[alertView show];


 
4. 로그인(ID)과 비밀번호 입력 스타일

UIAlertView *alertView = [[UIAlertView allocinitWithTitle:@"로그인과 비밀번호 입력 스타일"

                                                    message:@"로그인과 비밀번호를 입력하세요."

                                                   delegate:self

                                          cancelButtonTitle:@"취소"

                                          otherButtonTitles:@"확인"nil];

alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;

[alertView show];


이 경우 텍스트 필드에 입력된 데이터의 접근은 다음과 같이 한다.

UITextField *loginField = [alertView textFieldAtIndex:0];

NSLog(@"Login input: %@",loginField.text);

    

UITextField *passwordField = [alertView textFieldAtIndex:1];

NSLog(@"Password input: %@",passwordField.text);

 


또한, UIAlertViewDelegate에도 alertViewShouldEnableFirstOtherButton: 메서드가 추가 되었다. 이 메서드를 이용하여 다음과 같이 버튼을 동적으로 enabling/desabling 시킬 수 있다.

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView

{

    UITextField *textField = [alertView textFieldAtIndex:0];

    if ([textField.text length] == 0)

    {

        return NO;

    }

    

    return YES;

}


 



Posted by 다오나무