UIKit框架-07.UIAlertController使用
来源:互联网 发布:手机视频保密软件 编辑:程序博客网 时间:2024/05/17 21:55
1.UIAlertController概述
- UIAlertController是IOS 8.0之后出现的新特性,它同时结合了UIAlertView和UIActionSheet的功能
- IOS 8.0之前我们还是需要使用UIAlertView和UIActionSheet,完成弹框功能
2.使用UIAlertController实现UIAlertView效果
- 实现UIAlerView
-(void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{ // 1.创建UIAlertControllerStyleAlert样式的UIAlertController UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"标题" message:@"正文" preferredStyle:UIAlertControllerStyleAlert]; // 2.创建按钮 /* UIAlertActionStyleDefault = 0, //默认样式 UIAlertActionStyleCancel, // 字体加粗 UIAlertActionStyleDestructive // 强调,字体标红 */ UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"按钮1" style:UIAlertActionStyleCancel handler:^(UIAlertAction * __nonnull action) {// 点击事件 NSLog(@"action1被点击了"); }]; UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"按钮2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * __nonnull action) {// 点击事件 NSLog(@"action2被点击了"); }]; UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"按钮3" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * __nonnull action) {// 点击事件 NSLog(@"action3被点击了"); }]; // 2.1 添加按钮 [alertVc addAction:action1]; [alertVc addAction:action2]; [alertVc addAction:action3]; // 3.添加文本框 [alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) { NSLog(@"文本框1"); }]; [alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) { NSLog(@"文本框2"); textField.secureTextEntry = YES;//设置密文 }]; [alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) { NSLog(@"文本框3"); }]; //4.添加显示控制器方法 [self presentViewController:alertVc animated:YES completion:nil];}
效果图
注意:我们使用UIAlertView最多只能创建2个文本输入框,而实用UIAlertController可以创建多个文本输入框
3.实现UIActionSheet效果
- 实现UIActionSheet
-(void)setAlertview{ // 1.创建UIAlertControllerStyleAlert样式的UIAlertController UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"标题" message:@"正文" preferredStyle:UIAlertControllerStyleAlert]; // 2.创建按钮 /* UIAlertActionStyleDefault = 0, //默认样式 UIAlertActionStyleCancel, // 字体加粗 UIAlertActionStyleDestructive // 强调,字体标红 */ UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"按钮1" style:UIAlertActionStyleCancel handler:^(UIAlertAction * __nonnull action) {// 点击事件 NSLog(@"action1被点击了"); }]; UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"按钮2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * __nonnull action) {// 点击事件 NSLog(@"action2被点击了"); }]; UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"按钮3" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * __nonnull action) {// 点击事件 NSLog(@"action3被点击了"); }]; // 2.1 添加按钮 [alertVc addAction:action1]; [alertVc addAction:action2]; [alertVc addAction:action3]; // 3.添加文本框 [alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) { NSLog(@"文本框1"); }]; [alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) { NSLog(@"文本框2"); textField.secureTextEntry = YES;//设置密文 }]; [alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) { NSLog(@"文本框3"); }]; //4.添加显示控制器方法 [self presentViewController:alertVc animated:YES completion:nil];}
效果图
注意:
如果UIAlertController的样式是ActionSheet, 就不能添加输入框
reason: 'Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert'
UIAlertController继承UIController类,而UIAlertView和UIActionSheet继承UIView,所以要想在视图上显示UIAlertController,需要调用如下方法
- [self presentViewController:alertVc animated:YES completion:nil];
0 0
- UIKit框架-07.UIAlertController使用
- UIKit--UIAlertController
- UIKit框架-05.UIAlertView使用
- UIKit框架- 08.UIScrollView基本使用
- UIKit框架 - 06.UIActionSheet使用
- UIKit框架-01.UIView
- UIKit框架 -02.UILabel
- UIKit框架-04.UIButton
- UIKit框架 -09.UIPageControl
- UIKit框架-13.UINavigationController
- UIKit框架
- UIKit框架
- UIKit框架
- UIKit-UIAlertView,UIActionSheet,UIAlertController使用方法(完整版)
- uikit 中uialert 的使用方法UIAlertController
- UIAlertController使用
- UIAlertController 使用
- UIAlertController使用
- Game Programming Patterns-简介
- ITOO级联删除主从表
- Swift值字典使用
- CUDA初学整理
- Cocos2d-x vector使用遇到问题。
- UIKit框架-07.UIAlertController使用
- 正向代理,反向代理,透明代理 简介
- 提升方式创建自己的QT控件(弹出式菜单按钮)
- 标准C++中有四个类型转换符:static_cast、dynamic_cast、reinterpret_cast、和 const_cast
- 1164_上班路线
- 按照字节数截取一个字符串,“abc你好” 如果截取到半个中文,舍弃,比如 截取4个字节 abc。截取 5个字节abc你
- 黑马程序员--高薪技术--反射
- Android中ImageButton边框完全填充
- Spring学习笔记——Spring实例化Bean的三种方式