美文网首页技术
UIAlertController的使用

UIAlertController的使用

作者: 代码干货 | 来源:发表于2015-06-28 16:48 被阅读398次

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

一下是在UIAlertController中非常有用的方法
<pre>
//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;
    </pre>
  • 创建一个简单的Alert Dialog



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

    [self presentViewController:alert animated:YES completion:nil];
    </pre>

  • 2创建一个alert Dialog with action



    <pre>
    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];
    </pre>

  • 3 创建一个actionSheet with action



    <pre>
    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];
    </pre>

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



      <pre>
      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];
    </pre>

相关文章

网友评论

    本文标题:UIAlertController的使用

    本文链接:https://www.haomeiwen.com/subject/uqmeqttx.html