彈跳式視窗的用法

在 iOS 裡彈跳式視窗主要分為由下方昇起的 UIActionSheet 以及Dialog 類型的 UIAlertView

iOS8起 UIAlertView 已 deprecated, 改統一使用 UIAlertController

用法範例如下

 

Simple alert example:
UIAlertController * alert=   [UIAlertController alertControllerWithTitle:@"Password required" message:@"Please enter password" preferredStyle:UIAlertControllerStyleAlert];
     
[self presentViewController:alert animated:YES completion:nil];
Alert with ok button example
UIAlertController * alert=   [UIAlertController
                                alertControllerWithTitle:@"Name required"
                                message:@"Please input name"
                                preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okAction = [UIAlertAction
                       actionWithTitle:@"OK"
                       style:UIAlertActionStyleDefault
                       handler:^(UIAlertAction * action)
                       {
                           [alert dismissViewControllerAnimated:NO completion:nil];
                           // do something
                       }];
[alert addAction:okAction];

[self presentViewController:alert animated:YES completion:nil];
ActionSheet example
UIAlertController * view=   [UIAlertController
                             alertControllerWithTitle:@"Select Sex"
                             message:@""
                             preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction* male = [UIAlertAction
                       actionWithTitle:@"Male"
                       style:UIAlertActionStyleDefault
                       handler:^(UIAlertAction * action)
                       {
                           [view dismissViewControllerAnimated:NO completion:nil];
                           // do something
                       }];
UIAlertAction* female = [UIAlertAction
                         actionWithTitle:@"Roaster"
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {
                             [view dismissViewControllerAnimated:NO completion:nil];
                             // do something
                         }];

UIAlertAction* other = [UIAlertAction
                        actionWithTitle:@"Individual"
                        style:UIAlertActionStyleDefault
                        handler:^(UIAlertAction * action)
                        {
                            [view dismissViewControllerAnimated:NO completion:nil];
                            // do something
                        }];


UIAlertAction* cancel = [UIAlertAction
                         actionWithTitle:@"Cancel"
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {
                             [view dismissViewControllerAnimated:YES completion:nil];
                         }];

[view addAction:male];
[view addAction:female];
[view addAction:other];
[view addAction:cancel];
[self presentViewController:view animated:YES completion:nil];