当前位置: 首页 > news >正文

【iOS】UI——关于UIAlertController类(警告对话框)

目录

  • 前言
  • 关于UIAlertController
  • 具体操作及代码实现
  • 总结

前言

  在UI的警告对话框的学习中,我们发现UIAlertView在iOS 9中已经被废弃,我们找到UIAlertController来代替UIAlertView实现弹出框的功能,从而有了这篇关于UIAlertController的学习笔记。

关于UIAlertController

  UIAlertController 是 iOS SDK 中提供的一个强大且灵活的类,它可以用来显示警告框或者动作表。UIAlertController 取代了早期的 UIAlertView 和 UIActionSheet 类,提供了更加统一和灵活的界面。

具体操作及代码实现

//创建一个UIAlertController对象
//P1:弹出框的标题  P2弹出框的内容  
//P3:弹出的警告框的样式为UIAlertControllerStyleAlert(即中心弹出的警告框)
UIAlertController* alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"这是消息" preferredStyle:UIAlertControllerStyleAlert];//添加“确认”动作按钮到控制器上
//P1:标题文字  P2:动作样式,有三种动作样式:常规(default)、取消(cancel)以及警示(destruective)
//P3:用户选中并点击该动作时,所执行的代码
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {// 用户点击确认按钮后执行的代码
}];
//将动作按钮添加到alertController视图上
[alertController addAction:defaultAction];//添加“取消”动作按钮到控制器上
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {// 用户点击取消按钮后执行的代码
}];
//将动作按钮添加到alertController视图上
[alertController addAction:cancelAction];//将警告框显示出来
[self presentViewController:alertController animated:YES completion:nil];

  这个示例创建了一个 UIAlertController,并设置了标题、消息以及样式。然后,我们创建了两个 UIAlertAction,一个是默认的操作,另一个是取消操作(有三种动作样式:常规(default)、取消(cancel)以及警示(destruective))。这两个操作都有处理器,所以当用户点击这些按钮时,会执行相应的代码块。最后,我们使用 presentViewController:animated:completion: 方法来显示警告框。

  将以上代码放进xcode的"ViewController.h"文件的viewDidLoad函数中,运行后你会发现,模拟器屏幕上无任何显示,这是因为:

在 iOS 开发中,警告对话框(UIAlertController)不能直接被呈现,必须在某个存在的视图控制器(UIViewController)上呈现。这是因为UIAlertController实际上是一个视图控制器,而所有的视图控制器都需要一个父视图控制器才能被用户看到。

  为了看到警告框的效果,我们另外创建一个UIButton对象,用来触发警告框。

UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];btn.frame = CGRectMake(100, 100, 80, 80);btn.backgroundColor = [UIColor cyanColor];[self.view addSubview:btn];[btn addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchUpInside];

将UIAlertController相关操作放在btn的press函数里即可。完整代码如下:

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];btn.frame = CGRectMake(100, 100, 80, 80);btn.backgroundColor = [UIColor cyanColor];[self.view addSubview:btn];[btn addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchUpInside];
}- (void) press {//创建一个UIAlertController对象//P1:弹出框的标题  P2弹出框的内容//P3:弹出的警告框的样式为UIAlertControllerStyleAlert(即中心弹出的警告框)UIAlertController* alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"这是消息" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];[alertController addAction:defaultAction];UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];[alertController addAction:cancelAction];[self presentViewController:alertController animated:YES completion:nil];
}@end

按下btn按钮后的运行结果:
在这里插入图片描述

总结

  当我们需要显示一个警告对话框时,我们要在当前的视图控制器上呈现它;如果当前不在视图控制器中,但是需要显示警告对话框,我们需要获取到当前的视图控制器,或者创建一个新的视图控制器来呈现警告对话框。

相关文章:

  • 数据总线、位扩展、字长
  • 【三十三】springboot+序列化实现返回值脱敏和返回值字符串时间格式化问题
  • 【python报错】list indices must be integers or slices, not tuple
  • 数学+思维,CF1056B - Divide Candies
  • 网络安全快速入门(十五)(下)手动创建用户及su,sudo命令
  • 简单使用phpqrcode 生成二维码图片
  • 代码随想录算法训练营第36期DAY50
  • Docker 进入指定容器内部(以Mysql为例)
  • 详解linux设备下的/dev/null
  • 微信小程序怎么进行页面传参
  • 大学数字媒体艺术设计网页设计试题及答案,分享几个实用搜题和学习工具 #媒体#职场发展
  • 12寸晶圆厂建设概述
  • Javascript全解(基础篇)
  • C语言详解(动态内存管理)2
  • Nvidia Jetson/Orin/算能 +FPGA+AI大算力边缘计算盒子:潍柴雷沃智慧农业无人驾驶
  • ES6--对象的扩展
  • JavaScript DOM 10 - 滚动
  • Linux快速复制或删除大量小文件
  • Python学习之路16-使用API
  • 彻底搞懂浏览器Event-loop
  • 分布式熔断降级平台aegis
  • 服务器从安装到部署全过程(二)
  • 简单实现一个textarea自适应高度
  • 京东美团研发面经
  • 开放才能进步!Angular和Wijmo一起走过的日子
  • 前端面试之闭包
  • 浅谈JavaScript的面向对象和它的封装、继承、多态
  • 深入浏览器事件循环的本质
  • 移动端唤起键盘时取消position:fixed定位
  • Android开发者必备:推荐一款助力开发的开源APP
  • ​Python 3 新特性:类型注解
  • ​一些不规范的GTID使用场景
  • ## 1.3.Git命令
  • #{}和${}的区别?
  • #define与typedef区别
  • %check_box% in rails :coditions={:has_many , :through}
  • (1)(1.8) MSP(MultiWii 串行协议)(4.1 版)
  • (2)(2.4) TerraRanger Tower/Tower EVO(360度)
  • (22)C#传智:复习,多态虚方法抽象类接口,静态类,String与StringBuilder,集合泛型List与Dictionary,文件类,结构与类的区别
  • (C语言)逆序输出字符串
  • (二)springcloud实战之config配置中心
  • (论文阅读31/100)Stacked hourglass networks for human pose estimation
  • (求助)用傲游上csdn博客时标签栏和网址栏一直显示袁萌 的头像
  • (一)pytest自动化测试框架之生成测试报告(mac系统)
  • (一)VirtualBox安装增强功能
  • (转) Android中ViewStub组件使用
  • ***测试-HTTP方法
  • ***通过什么方式***网吧
  • .Net Winform开发笔记(一)
  • .NET 的静态构造函数是否线程安全?答案是肯定的!
  • .NET 设计模式初探
  • .NET_WebForm_layui控件使用及与webform联合使用
  • .NetCore发布到IIS
  • .net访问oracle数据库性能问题
  • .Net下的签名与混淆