OC之UIAlertController

SONG / 文 发表于2018-01-26 16:02 次阅读 uialertcontroller

IOS8之前警告框(UIAlertView)

UIAlertView *alertView =
[[UIAlertView alloc]initWithTitle:@"标题"
                        message:@"文本信息"
                        delegate:self
                        cancelButtonTitle:@"取消"
                        otherButtonTitles:@"确定", nil];
    //显示alertView
    [alertView show];

IOS8之后警告框(UIAlertController,二个以下会水平放置)

//UIAlertController风格:UIAlertControllerStyleAlert
UIAlertController *alertController = [UIAlertControlleralertControllerWithTitle:@"标题"message:@"文本信息"preferredStyle:UIAlertControllerStyleAlert ];

    //添加取消到UIAlertController中
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:cancelAction];

    //添加确定到UIAlertController中
UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:OKAction];
[self presentViewController:alertController animated:YES completion:nil];

IOS8之前的操作表(UIAlertSheet)

UIActionSheet *actionSheet =
[[UIActionSheet alloc]initWithTitle:@"旧的的Action Sheet"
                         delegate:self
                         cancelButtonTitle:@"取消样式"
                         destructiveButtonTitle:@"告警样式"
                         otherButtonTitles:@"默认样式", nil];
    [actionSheet showInView:self.view];

IOS8之后操作表(UIAlertController)

UIAlertController *alertController =
[UIAlertController alertControllerWithTitle:
            @"标准的Action Sheet样式"
            message:nil   
           preferredStyle:UIAlertControllerStyleActionSheet];
    //取消样式:style:UIAlertActionStyleCancel
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消样式" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:cancelAction];
    //告警样式:style:UIAlertActionStyleDestructive
UIAlertAction *moreAction = [UIAlertAction actionWithTitle:@"告警样式" style:UIAlertActionStyleDestructive handler:nil];
 [alertController addAction:moreAction];
    //默认样式:style:UIAlertActionStyleDefault
UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"默认样式" style:UIAlertActionStyleDefault handler:nil];
 [alertController addAction:OKAction];

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

文本框弹窗

// 1.创建UIAlertController
UIAlertController *alertController =
[UIAlertController alertControllerWithTitle:@"标题"                                  message:@"文本信息"
     preferredStyle:UIAlertControllerStyleAlert];
// 2.1 添加文本框
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {textField.placeholder = @"username";}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"password";
        textField.secureTextEntry = YES;}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"Cancel Action");}];

UIAlertAction *loginAction = [UIAlertAction actionWithTitle:@"Login" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UITextField *userName = alertController.textFields.firstObject;
UITextField *password = alertController.textFields.lastObject;

        // 输出用户名 密码到控制台
        NSLog(@"username is %@, password is %@",userName.text,password.text);

// 2.3 添加按钮
    [alertController addAction:cancelAction];
    [alertController addAction:loginAction];

    // 3.显示警报控制器
    [self presentViewController:alertController animated:YES completion:nil];

pad添加操作表(瞄点,在点击的按钮附近弹出):

UIPopoverPresentationController *popover = alertController.popoverPresentationController;
    if (popover)
    {
        popover.sourceView = sender;
        popover.sourceRect = sender.bounds;
        popover.permittedArrowDirections = UIPopoverArrowDirectionAny;
    }
收藏 赞 (0) 踩 (0)