转自:https://blog.csdn.net/deng0zhaotai/article/details/53887508

通过uialertcontroller实现三种简易弹框

(一)警告类

  1. - (IBAction)showAlert:(UIButton *)sender {
  2. //显示提示框
  3. //过时
  4. // UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
  5. // [alert show];
  6. UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Title"
  7. message:@"This is an alert."
  8. preferredStyle:UIAlertControllerStyleAlert];
  9.  
  10. UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
  11. handler:^(UIAlertAction * action) {
  12. //响应事件
  13. NSLog(@"action = %@", action);
  14. }];
  15. UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
  16. handler:^(UIAlertAction * action) {
  17. //响应事件
  18. NSLog(@"action = %@", action);
  19. }];
  20.  
  21. [alert addAction:defaultAction];
  22. [alert addAction:cancelAction];
  23. [self presentViewController:alert animated:YES completion:nil];
  24. }

(二)带输入框警告类

  1. - (IBAction)showList:(UIButton *)sender {
  2. //提示框添加文本输入框
  3. UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Title"
  4. message:@"This is an alert."
  5. preferredStyle:UIAlertControllerStyleAlert];
  6.  
  7. UIAlertAction* okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
  8. handler:^(UIAlertAction * action) {
  9. //响应事件
  10. //得到文本信息
  11. for(UITextField *text in alert.textFields){
  12. NSLog(@"text = %@", text.text);
  13. }
  14. }];
  15. UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel
  16. handler:^(UIAlertAction * action) {
  17. //响应事件
  18. NSLog(@"action = %@", alert.textFields);
  19. }];
  20. [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  21. textField.placeholder = @"登录";
  22. }];
  23. [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  24. textField.placeholder = @"密码";
  25. textField.secureTextEntry = YES;
  26. }];
  27.  
  28. [alert addAction:okAction];
  29. [alert addAction:cancelAction];
  30. [self presentViewController:alert animated:YES completion:nil];
  31.  
  32. }

(三)操作列表类

  1. - (IBAction)showSheet:(UIButton *)sender {
  2. //显示弹出框列表选择
  3. UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Title"
  4. message:@"This is an Sheet."
  5. preferredStyle:UIAlertControllerStyleActionSheet];
  6.  
  7. UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
  8. handler:^(UIAlertAction * action) {
  9. //响应事件
  10. NSLog(@"action = %@", action);
  11. }];
  12. UIAlertAction* deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive
  13. handler:^(UIAlertAction * action) {
  14. //响应事件
  15. NSLog(@"action = %@", action);
  16. }];
  17. UIAlertAction* saveAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault
  18. handler:^(UIAlertAction * action) {
  19. //响应事件
  20. NSLog(@"action = %@", action);
  21. }];
  22. [alert addAction:saveAction];
  23. [alert addAction:cancelAction];
  24. [alert addAction:deleteAction];
  25. [self presentViewController:alert animated:YES completion:nil];
  26. }

UIAlert的更多相关文章

  1. UIAlertView' is deprecated: first deprecated in iOS 9.0 - UIAlertView is deprecated. Use UIAlert

    UIAlertController * cancleAlertController = [UIAlertController alertControllerWithTitle:nil message: ...

  2. [IOS UIalert模版]

    1.alertview创建 UIAlertView *alert; alert = [[UIAlertView alloc] initWithTitle:@"提示" message ...

  3. UIActionSheet和UIAlert

    UIActionSheet: 首先,在.h文件中添加Protocol,(Protocol相当于Java中的interface) @interface ActionSheetViewController ...

  4. UIkit框架之UIalert(iOS 9之后就不用这个了)

    IOS中UIAlertView(警告框)常用方法总结 一.初始化方法 - (instancetype)initWithTitle:(NSString *)title message:(NSString ...

  5. 【iOS开发-56】案例BUG:button的enabled、控件的userInteractionEnabled以及两种提示框UIAlert和UIActionSheet

    接上述案例找BUG:[iOS开发-51]案例学习:动画新写法.删除子视图.视图顺序.延迟方法.button多功能使用方法及icon图标和启动页设置 (1)BUG:答案满了就不能再点击optionbut ...

  6. IOS之UIAlert​Controller

    你知道 UIAlertView.UIActionSheet (以及它们各自的 delegate protocols) 在 iOS 8 中已经被废弃了吗? 这是真的.在你的代码中按住 ⌘ 点击 UIAl ...

  7. iOS开发系列--视图切换

    概述 在iOS开发中视图的切换是很频繁的,独立的视图应用在实际开发过程中并不常见,除非你的应用足够简单.在iOS开发中常用的视图切换有三种,今天我们将一一介绍: UITabBarController ...

  8. iOS开发——高级篇——iOS中常见的设计模式(MVC/单例/委托/观察者)

    关于设计模式这个问题,在网上也找过一些资料,下面是我自己总结的,分享给大家 如果你刚接触设计模式,我们有好消息告诉你!首先,多亏了Cocoa的构建方式,你已经使用了许多的设计模式以及被鼓励的最佳实践. ...

  9. IOS要用到的东西

    code4app.com 这网站不错,收集各种 iOS App 开发可以用到的代码示例 cocoacontrols.com/ 英文版本的lib收集 objclibs.com/ 精品lib的收集网站 h ...

随机推荐

  1. hihttps教你在Wireshark中提取旁路https解密源码

    大家好,我是hihttps,专注SSL web安全研究,今天本文就是教大家怎样从wireshark源码中,提取旁路https解密的源码,非常值得学习和商业应用. 一.旁路https解密条件 众所周知, ...

  2. 一个有意思的js块作用域问题

    1.问题 首先把问题放出来,昨天看了一个掘友发的一个问题,然后跟我同事一起研究了一下,没找出来是为什么,然后我回来一直在想为什么,然后各种找资料研究,从各个方面找为什么,比如js上下文,作用域,js垃 ...

  3. java8 base64使用

    java 1.8中引入了Base64,不在需要引入第三方库就可以使用base64了. 在需要用到base64进行加密解密的时候就可以使用了 String text = "base64 in ...

  4. Object.keys方法详解

    一.官方解释 Object.keys() 方法会返回一个由一个给定对象的自身可枚举属性组成的数组,数组中属性名的排列顺序和使用 for...in 循环遍历该对象时返回的顺序一致 .如果对象的键-值都不 ...

  5. linux下安装配置go语言环境

    1,golang中国下载go源码  http://www.golangtc.com/download  请对应系统版本号,linux-amd64.tar.gz为64位系统(推荐) ,linux-386 ...

  6. el-table实现行列拖拽

    element ui 表格没有自带的拖拽排序的功能,只能借助第三方插件Sortablejs来实现. 实现步骤: 安装Sortable.js npm install sortablejs --save ...

  7. springboot项目目录结构

    idea新建springboot项目 按默认下一步至完成,默认目录结构如下 pom.xml文件内容如下 <?xml version="1.0" encoding=" ...

  8. Coins POJ - 1742

    给出硬币面额及每种硬币的个数,求从1到m能凑出面额的个数. Input 多组数据,每组数据前两个数字为n,m.n表示硬币种类数,m为最大面额,之后前n个数为每种硬币的面额,后n个数为相应每种硬币的个数 ...

  9. bugku 各种·绕过

    点开是一段PHP的代码,先来审计一波代码. 发现将uname和passwd用sha1进行了加密,那么我们只要绕过这个函数构造相等就可以了. 可以使这两个值sha1的值相等,但他们本身的值又不等.(想详 ...

  10. 一个神秘现象引发对beego框架的思考

    小强最近在项目中遇到了一个很奇怪的问题:在整改日志规范时,为了避免影响现有的代码结构以及改动尽可能小的前提下,在调用记日志的SDK处将某一个字段值首字母改为大写,代码示例如下: fmt.Println ...