본문 바로가기
옛날

IOS 9 이상 UIAlertController [IOS]

by 차가운게 조아 2015. 12. 15.

소스다운로드 > https://github.com/cpromise/UIAlertControllerExample

이 예제에 사용된 소스 : initial commit을 다운로드 받으시면 됩니다.



<UIAlertController 이미지>


iOS8부터 alertView가 deprecated되었죠?

우리는 기계적으로 애플의 노예이기 때문에 UIAlertController를 사용해야 합니다.

절이 싫으면 중이 떠나야하는데.. 떠나면 이 추운 날에 굶어야하니까 일단 남아보도록 하죠..



UIAlertController에는 크게 3가지로 나뉩니다.


그럼 나눠보도록 할게요.



1. No Button, No action.




UIAlertController * alert=   [UIAlertController

                              alertControllerWithTitle:@"My Title"

                              message:@"Will dismiss in 2 seconds."

                              preferredStyle:UIAlertControllerStyleAlert];

 

[self presentViewController:alert animated:YES completion:nil];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

    [alert dismissViewControllerAnimated:YES completion:nil];

});









2. 많이 보시던 일반적인 스타일









UIAlertController * alert=   [UIAlertController

                              alertControllerWithTitle:@"Yes or No?"

                              message:@"결심했어!"

                              preferredStyle:UIAlertControllerStyleAlert];

 

UIAlertAction* ok = [UIAlertAction

                     actionWithTitle:@"OK"

                     style:UIAlertActionStyleDefault

                     handler:^(UIAlertAction * action)

                     {

                         [alert dismissViewControllerAnimated:YES completion:nil];

                          

                     }];

UIAlertAction* cancel = [UIAlertAction

                         actionWithTitle:@"Cancel"

                         style:UIAlertActionStyleDefault

                         handler:^(UIAlertAction * action)

                         {

                             [alert dismissViewControllerAnimated:YES completion:nil];

                              

                         }];

 

[alert addAction:ok];

[alert addAction:cancel];

 

[self presentViewController:alert animated:YES completion:nil];









3. Alert가 화면 아랫부분에 노출




UIAlertController * view=   [UIAlertController

                             alertControllerWithTitle:@"Yes or No?"

                             message:@"결심했어!"

                             preferredStyle:UIAlertControllerStyleActionSheet];

 

UIAlertAction* ok = [UIAlertAction

                     actionWithTitle:@"OK"

                     style:UIAlertActionStyleDefault

                     handler:^(UIAlertAction * action)

                     {

                         //Do some thing here

                         [view dismissViewControllerAnimated:YES completion:nil];

                          

                     }];

UIAlertAction* cancel = [UIAlertAction

                         actionWithTitle:@"Cancel"

                         style:UIAlertActionStyleDefault

                         handler:^(UIAlertAction * action)

                         {

                             [view dismissViewControllerAnimated:YES completion:nil];

                              

                         }];

 

 

[view addAction:ok];

[view addAction:cancel];

[self presentViewController:view animated:YES completion:nil];






4. TextField를 갖는 AlertController



UIAlertController * alert=   [UIAlertController

                              alertControllerWithTitle:@"Login"

                              message:@"Enter User Info"

                              preferredStyle:UIAlertControllerStyleAlert];

 

UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault

                                           handler:^(UIAlertAction * action) {

                                               //Do Some action here

                                                

                                           }];

UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault

                                               handler:^(UIAlertAction * action) {

                                                   [alert dismissViewControllerAnimated:YES completion:nil];

                                               }];

 

[alert addAction:ok];

[alert addAction:cancel];

 

[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {

    textField.placeholder = @"Username";

}];

[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {

    textField.placeholder = @"Password";

    textField.secureTextEntry = YES;

}];

 

[self presentViewController:alert animated:YES completion:nil];



'옛날' 카테고리의 다른 글

http post get 전송  (0) 2015.12.15
user agent [IOS]  (1) 2015.12.15
WebViewClient [Android]  (0) 2015.12.15
UIWebView user-agent [IOS]  (0) 2015.12.15
랜선 제작  (0) 2015.12.14