在iOS8以后,UIAlertView就开始被抛弃了。

取而代之是UIAlertController

以前是警示框这样写:

  1. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入用户名、密码和服务器" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
  2.  
  3. [alert show];

效果如图:

现在是这样写:

  1. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入用户名、密码和服务器" preferredStyle:UIAlertControllerStyleAlert];
  2. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  3. NSLog(@"点击了取消按钮");
  4. }];
  5. UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  6. NSLog(@"点击了确定按钮");
  7. }];
  8.  
  9. [alertController addAction:cancelAction];
  10. [alertController addAction:OKAction];
  11.  
  12. [self presentViewController:alertController animated:YES completion:nil];

效果如图:

添加多个Action会自动向下排列

上面代码中再添加一个

  1. UIAlertAction *DesAction = [UIAlertAction actionWithTitle:@"Destory" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  2.  
  3. NSLog(@"点击了Destory按钮");
  4.  
  5. }];
  6.  
  7. [alertController addAction:DesAction];

如果提示菜单中有“取消”按钮的话,那么它永远都会出现在菜单的底部

  1. //冻结确定按钮
  2. OKAction.enabled = NO;

添加文本框

  1. [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  2. textField.placeholder = @"Email";
  3. textField.keyboardType = UIKeyboardTypeEmailAddress;
  4. }];
  5. [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  6. textField.placeholder = @"Password";
  7. textField.secureTextEntry = YES;
  8. }];

UIActionSheet也用这个取代了。。方法是在初始化的时候把类型改一下就OK。但是UIActionSheet是不能加文本框的!!

  1. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入用户名、密码和服务器" preferredStyle:UIAlertControllerStyleActionSheet];

P.S 用通知监听用户名和密码输入, 这里有篇介绍得很好的文章:

http://blog.csdn.net/lengshengren/article/details/39896037

UIAlertController的使用,代替UIAlertView和UIActionSheet的更多相关文章

  1. iOS8以后UIAlertView和UIActionSheet两种alert页面都将通过UIAlertController来创建

    1. Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated. ...

  2. UIAlertView、UIActionSheet兼容iOS8

    链接地址:http://blog.csdn.net/nextstudio/article/details/39959895?utm_source=tuicool 1.前言 iOS8新增了UIAlert ...

  3. UIAlertView及UIActionSheet 在ios8极其以下版本的兼容问题解决方案

    本文转载至 http://www.aichengxu.com/view/35326 UIAlertView及UIActionSheet在ios8中被放弃,其功能将完全由UIAlertControlle ...

  4. iOS 8 中 UIAlertView 和 UIActionSheet 河里去了?

    iOS 8 中 UIAlertView 和 UIActionSheet 河里去了? 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商 ...

  5. UIAlertView、 UIActionSheet

    一.UIAlertView. UIActionSheet都是ios系统自带的弹出式对话框,当UIAlertView或UIActionSheet弹出来时用户无法与应用界面中的其它控件交互,UIAlert ...

  6. iOS开发——UI篇Swift篇&UIAlertView/UIActionSheet

    UIAlertView/UIActionSheet UIAlertView //一个按钮的提醒 @IBAction func oneButtonAler() { //创建单一按钮提醒视图 let on ...

  7. 用block将UIAlertView与UIActionSheet统一起来

    用block将UIAlertView与UIActionSheet统一起来 效果 1. 将代理方法的实例对象方法转换成了类方法使用 2. 要注意单例block不要长期持有,用完就释放掉 源码 https ...

  8. iOS:简单使用UIAlertVIew和UIActionSheet

    做iOS开发的同学想必都用过UIAlertVIew或者UIActionSheet.UIAlertVIew 可以弹出一个出现在屏幕中间的提示视图,给用户展示信息,并让用户自己选择操作,UIActionS ...

  9. iOS - 提示信息 - UIAlertView、UIActionSheet、UIAlertController的实际应用

    1.UIAlertView(屏幕中央弹出框)(不需要服从代理) UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@" ...

随机推荐

  1. 猫猫学iOS 之微博项目实战(2)微博主框架-自己定义导航控制器NavigationController

    猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243?viewmode=contents 一:加入导航控制器 上一篇博 ...

  2. js性能优化之函数节流(分流函数)

    函数节流的原理 比如我们在window.onresize事件中要打印当前浏览器窗口的大小,在我们通过拖拽来改变窗口大小时候,打印窗口大小这个工作1s就运行了10次.而实际上我们只需要2次或者3次. 比 ...

  3. Oracle APEX 4.2安装和配置

    A standard Oracle 11.2.0.3 database installation comes bundled with Application Express (APEX) 3.2.1 ...

  4. activiti自己定义流程之自己定义表单(一):环境配置

    先补充说一下自己定义流程整个的思路,自己定义流程的目的就是为了让一套代码解决多种业务流程.比方请假单.报销单.採购单.协作单等等.用户自己来设计流程图. 这里要涉及到这样几个基本问题,一是不同的业务需 ...

  5. LoadRunner压测时,出现的问题汇总

    [问题]Error -10776  Failed to find .cfg file 错误分析:在loadrunner打开脚本的情况下,运行磁盘清理工具,导致运行打开的脚本时,提示Mdrv error ...

  6. serverSpeed是一个android手机端到服务器间udp/tcp对比测速软件

    https://github.com/eltld/serverSpeed https://github.com/c-wind/serverSpeed https://github.com/PeterK ...

  7. VUEX action解除页面耦合

    最近项目中需要用到vue+vuex来实现登出跳转功能,老大指派任务要用action解除页面耦合,刚从vue深渊晕晕乎乎爬出来的我是一脸懵逼啊...啥是解除耦合...网上vuex的资料太少了,vuex手 ...

  8. Django的缓存,序列化,ORM操作的性能

    1,缓存:把数据先保存在某个地方,下次再读取的时候不用再去原位置读取

  9. ubuntu查看文件的权限

    查看linux文件的权限: 查看path路径下名为filename的文件或文件夹的权限: ls -l path/filename ls -l path/filename 查看path路径下的所有文件的 ...

  10. Koa2学习(六)使用koa-router

    Koa2学习(六)使用koa-router 配置简单路由 引入中间件 配置需要的路由 通过app.use注册路由 const Koa = require('koa') const app = new ...