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

通过uialertcontroller实现三种简易弹框

(一)警告类

- (IBAction)showAlert:(UIButton *)sender {
//显示提示框
//过时
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
// [alert show];
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Title"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//响应事件
NSLog(@"action = %@", action);
}];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//响应事件
NSLog(@"action = %@", action);
}]; [alert addAction:defaultAction];
[alert addAction:cancelAction];
[self presentViewController:alert animated:YES completion:nil];
}

(二)带输入框警告类

- (IBAction)showList:(UIButton *)sender {
//提示框添加文本输入框
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Title"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//响应事件
//得到文本信息
for(UITextField *text in alert.textFields){
NSLog(@"text = %@", text.text);
}
}];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
//响应事件
NSLog(@"action = %@", alert.textFields);
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"登录";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"密码";
textField.secureTextEntry = YES;
}]; [alert addAction:okAction];
[alert addAction:cancelAction];
[self presentViewController:alert animated:YES completion:nil]; }

(三)操作列表类

- (IBAction)showSheet:(UIButton *)sender {
//显示弹出框列表选择
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Title"
message:@"This is an Sheet."
preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
//响应事件
NSLog(@"action = %@", action);
}];
UIAlertAction* deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * action) {
//响应事件
NSLog(@"action = %@", action);
}];
UIAlertAction* saveAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//响应事件
NSLog(@"action = %@", action);
}];
[alert addAction:saveAction];
[alert addAction:cancelAction];
[alert addAction:deleteAction];
[self presentViewController:alert animated:YES completion:nil];
}

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. Spring boot 梳理 - SpringApplication

    简单启动方式 public static void main(String[] args) { SpringApplication.run(MySpringConfiguration.class, a ...

  2. idea 自动生成并跳转单元测试

    在要测试的类上按快捷键ctrl + shift + t,选择Create New Test,在出现的对话框的下面member内勾选要测试的方法,点击ok 或者点击菜单栏Navigate–>tes ...

  3. 使用.Htaccess文件实现301重定向常用的七种方法

    使用.Htaccess文件实现301重定向常用的七种方法   301重定向对广大站长来说并不陌生,从网站建设到目录优化,避免不了对网站目录进行更改,在这种情况下用户的收藏夹里面和搜索引擎里面可能保存的 ...

  4. 从零开始入门 K8s | 应用存储和持久化数据卷:存储快照与拓扑调度

    作者 | 至天 阿里巴巴高级研发工程师 一.基本知识 存储快照产生背景 在使用存储时,为了提高数据操作的容错性,我们通常有需要对线上数据进行 snapshot ,以及能快速 restore 的能力.另 ...

  5. Laravel .env 多环境配置文件

    项目开发中,通常会有本地开发环境.内网测试环境.线上真实环境.这三种环境的配置通常都不尽相同,Laravel 可以通过环境变量  APP_ENV 的值来加载不同的 .env 配置文件.下面会介绍两种方 ...

  6. python语言程序设计基础(嵩天)第四章课后习题部分答案

    p121: *题4.1:猜数字游戏.在程序中预设一个0~9之间的整数,让用户通过键盘输入所猜的数,如果大于预设的数,显示“遗憾,太大了!”:小于预设的数,显示“遗憾,太小了!”,如此循环,直至猜中该数 ...

  7. mysql配置默认字符集为UTF8mb4

    [client] default-character-set=utf8mb4 [mysqld] character-set-server = utf8mb4 collation-server = ut ...

  8. sql中的 where 、group by 和 having 用法解析

    --sql中的 where .group by 和 having 用法解析 --如果要用到group by 一般用到的就是“每这个字” 例如说明现在有一个这样的表:每个部门有多少人 就要用到分组的技术 ...

  9. 初探内核之《Linux内核设计与实现》笔记上

    内核简介  本篇简单介绍内核相关的基本概念. 主要内容: 单内核和微内核 内核版本号 1. 单内核和微内核   原理 优势 劣势 单内核 整个内核都在一个大内核地址空间上运行. 1. 简单.2. 高效 ...

  10. 计算几何基础算法几何C++实现

    This file is implementation of Common Common Computational Geometry Algorithms.Please please pay att ...