IOS UIAlertController 弹框 (ios 9.0 后代替了UIAlertView弹框 和 UIActionSheet下弹框)

在IOS 9.0 后 苹果官方宣布不再或不推荐使用UIAlertView 和 UIActionSheet 由UIAlertController进行代替两者 用控制器将两者合二为一 很简单 方便 下面就是关于UIAlertView的常用方法

一)新旧对比:
标准的Alert样式:
这里写图片描述
旧方法:UIAlertView:

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

新方法:UIAlertController:

  //UIAlertController风格:UIAlertControllerStyleAlert
  UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"主标题" 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]

标准的Alert Sheet样式:
这里写图片描述
旧方法:UIActionSheet

  UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"标准的Action Sheet样式" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"了解更多"otherButtonTitles:@"原来如此", nil];
  [actionSheet showInView:self.view];

新方法: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];

二)新功能:
UIAlertController 并不只是对已有的 API 做了清理,而是进行了标准化归纳。以前,预设的样式闲置有很多(swizzling 虽然可以提供更多的功能但还是有很大风险)。UIAlertController 让以前看起来很神奇的事情变为了可能。

这种行为已经被UIAlertActionStyle所覆盖,共有三种类型:

style:UIAlertActionStyleDefault//对按钮应用标准样式
style:UIAlertActionStyleCancel//对按钮应用取消样式,即取消操作
style:UIAlertActionStyleDestructive//对按钮应用警示性样式,提示用户这样做可能会删除或者改变某些数据

这里写图片描述

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"主标题:标题党"message:@"子标题:提示信息" preferredStyle:UIAlertControllerStyleAlert ];
    //取消style:UIAlertActionStyleDefault
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:cancelAction];

    //style:UIAlertActionStyleDestructive(警告提示)
    UIAlertAction *rubbishAction = [UIAlertAction actionWithTitle:@"确定修改" style:UIAlertActionStyleDestructive handler:nil];
    [alertController addAction:rubbishAction];

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

三)弹出文本框

// 只有在alert情况下才可以添加文本框
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"用户名";
        textField.secureTextEntry = YES;
    }];

//    // 取出文本
//    UITextField *text = alertController.textFields.firstObject;
//    UIAlertAction *action = alertController.actions.firstObject;

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值