인디노트

iOS9 부터는 UIAlertController 사용 권장 본문

소스 팁/Objective C, Swift, iOS, macOS

iOS9 부터는 UIAlertController 사용 권장

인디개발자 2018. 1. 5. 08:11

소스다운로드 > 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가 화면 아랫부분에 노출


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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://rhammer.tistory.com/64 [고무망치]

반응형
Comments