UIAlertController的使用

在iOS 8中,苹果废弃了UIAlertView,UIActionSheet类,但仍然可用。你应该避免使用它,iOS的未来版本可能不支持它。
原文链接:UIAlertController Example in iOS
接下来我将交大家如何使用UIAlertController。
UIAlertController支持2中类型。使用UIAlertController你可以创建一个警告对话框(UIAlerview)或行动表(UIActionSheet).


typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0,
UIAlertControllerStyleAlert
} NS_ENUM_AVAILABLE_IOS(8_0);

一下是在UIAlertController中非常有用的方法


//Create UIAlertController

  • (instancetype)actionWithTitle:(NSString *)title style:(UIAlertActionStyle)style
    handler:(void (^)(UIAlertAction *action))handler;

//Adding an action

  • (void)addAction:(UIAlertAction *)action;

//Adding text filed to UIAlertController.This method is supported only for UIAlertControllerStyleAlert

  • (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;
  • 创建一个简单的Alert Dialog


    UIAlertController的使用_第1张图片


    UIAlertController * alert= [UIAlertController
    alertControllerWithTitle:@"My Title"
    message:@"Enter User Credentials"
    preferredStyle:UIAlertControllerStyleAlert];

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

  • 2创建一个alert Dialog with action


    UIAlertController的使用_第2张图片


    UIAlertController * alert= [UIAlertController
    alertControllerWithTitle:@"Info"
    message:@"You are using UIAlertController"
    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 创建一个actionSheet with action


    UIAlertController的使用_第3张图片


    UIAlertController * view= [UIAlertController
    alertControllerWithTitle:@"My Title"
    message:@"Select you Choice"
    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];

    1. Create an Alert dialog with username and password fields.


      UIAlertController的使用_第4张图片


      UIAlertController * alert= [UIAlertController
      alertControllerWithTitle:@"My Title"
      message:@"Enter User Credentials"
      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];

你可能感兴趣的:(UIAlertController的使用)