iOS UIAlertController

2023-05-16

运行环境:
Xcode7.2.1,iOS Simulator9.2
语言:
Objective-C、Swift

关于UIAlertController的使用,主要有三种不同的方式:

1.简单的UIAlertController形式;

简单的UIAlertController形式

2.带有输入框的UIAlertController;

带有输入框的UIAlertController

3.ActionSheet样式的UIAlertController。

ActionSheet样式的UIAlertController

下面依次说明一下三种方式的创建和使用。

1.简单的UIAlertController形式

Objective-C 版

// 创建一个UIAlertController, 命名为alertOne
UIAlertController *alertOne = [UIAlertController alertControllerWithTitle:@"Alert-1" message:@"这是第一个AlertController" preferredStyle:UIAlertControllerStyleAlert];

// 创建一个“取消”按钮
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
// 创建一个“确定”按钮
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
    // 在此写点击“确定”按钮的触发事件
    // ......
}];
// 将两个按钮添加到alertOne
[alertOne addAction:cancelAction];
[alertOne addAction:confirmAction];

// 弹出alertOne
[self presentViewController:alertOne animated:YES completion:nil];

Swift 版

// 创建一个UIAlertController,命名为alertOne
let alertOne = UIAlertController.init(title: "Alert-1", message: "这是第一个AlertController", preferredStyle: UIAlertControllerStyle.Alert)
// 创建一个“取消”按钮
let cancelAction = UIAlertAction.init(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
// 创建一个“确定”按钮
let confirmAction = UIAlertAction.init(title: "确定", style: UIAlertActionStyle.Default){ (action: UIAlertAction) -> Void in
    // 在此写点击“确定”按钮的触发事件
    // ......
    }
// 将两个按钮添加到alertOne
alertOne.addAction(cancelAction)
alertOne.addAction(confirmAction)

// 弹出alertOne
self.presentViewController(alertOne, animated: true, completion: nil)

2.带有输入框的UIAlertController

Objective-C 版

// 创建一个UIAlertController, 命名为alertTwo
UIAlertController *alertTwo = [UIAlertController alertControllerWithTitle:@"Alert-2" message:@"这是第二个AlertController" preferredStyle:UIAlertControllerStyleAlert];
// 给alertTwo添加一个输入框
[alertTwo addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textfield){
    textfield.placeholder = @"第一个输入框";
    textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
}];
// 给alertTwo再添加一个输入框
[alertTwo addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textfield){
    textfield.placeholder = @"第二个输入框";
    textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
}];
// 创建一个“取消”按钮
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
// 创建一个“确定”按钮
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
    // 输出文本框中的内容
    NSLog([alertTwo.textFields[0] text]);
    NSLog([alertTwo.textFields[1] text]);
}];
// 将两个按钮添加到alertTwo上
[alertTwo addAction:cancelAction];
[alertTwo addAction:confirmAction];

// 弹出alertTwo
[self presentViewController:alertTwo animated:YES completion:nil];

Swift 版

// 创建一个UIAlertController,命名为alertTwo
let alertTwo = UIAlertController.init(title: "Alert-2", message: "这是第二个AlertController", preferredStyle: UIAlertControllerStyle.Alert)
// 给alertTwo添加第一个输入框
alertTwo.addTextFieldWithConfigurationHandler{ (textfield: UITextField) -> Void in
    textfield.placeholder = "第一个输入框"
}
// 给alertTwo添加第二个输入框
alertTwo.addTextFieldWithConfigurationHandler{ (textfield: UITextField) -> Void in
    textfield.placeholder = "第二个输入框"
}
// 创建一个“取消”按钮
let cancelAction = UIAlertAction.init(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
// 创建一个“确定”按钮
let confirmAction = UIAlertAction.init(title: "确定", style: UIAlertActionStyle.Default){ (action: UIAlertAction) -> Void in
    // 在此写点击“确定”按钮的触发事件
    // ......
    print(alertTwo.textFields![0].text!)
    print(alertTwo.textFields![1].text!)
}
// 将两个按钮添加到alertTwo
alertTwo.addAction(cancelAction)
alertTwo.addAction(confirmAction)

// 弹出alertTwo
self.presentViewController(alertTwo, animated: true, completion: nil)

3.ActionSheet样式的UIAlertController

Objective-C 版

// 创建一个UIAlertController, 命名为alertThree, preferredStyle 设置为 UIAlertControllerStyleActionSheet
UIAlertController *alertThree = [UIAlertController alertControllerWithTitle:@"ActionSheet - 1" message:@"这是一个ActionSheet" preferredStyle:UIAlertControllerStyleActionSheet];
// 创建一个“取消”按钮
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
// 创建一个“确定”按钮
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
    // 在此写点击“确定”按钮的触发事件
    // ......
}];
// 将两个按钮添加到alertThree上
[alertThree addAction:cancelAction];
[alertThree addAction:confirmAction];

// 弹出alertThree
[self presentViewController:alertThree animated:YES completion:nil];

Swift 版

// 创建一个UIAlertController, 命名为alertThree, preferredStyle 设置为 ActionSheet
let alertThree = UIAlertController.init(title: "ActionSheet - 1", message: "这是一个ActionSheet", preferredStyle: UIAlertControllerStyle.ActionSheet)
// 创建一个“取消”按钮
let cancelAction = UIAlertAction.init(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
// 创建一个“确定”按钮
let confirmAction = UIAlertAction.init(title: "确定", style: UIAlertActionStyle.Default){ (action: UIAlertAction) -> Void in
    // 在此写点击“确定”按钮的触发事件
    // ......
}
// 将两个按钮添加到alertTwo
alertThree.addAction(cancelAction)
alertThree.addAction(confirmAction)

// 弹出alertThree
self.presentViewController(alertThree, animated: true, completion: nil)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iOS UIAlertController 的相关文章

随机推荐

  • 用中断实现流水灯

    include lt stm32f4xx h gt define GPIO Pin 6 0x0040 LED2 define GPIO Pin 7 0x0080 LED1 define PLL M 8 define PLL N 336 de
  • debian 无法使用apt-get解决办法

    刚装完debian xff0c 发现直接apt get是用不了的 xff0c 系统会提示你插入DVD xff0c 其实是源的配置问题 xff0c 只需要在apt的源里面修改下配置就可以了 打开apt源文件 sudo vi etc apt s
  • 蜗牛星际C单黑群晖加USB网卡做链路聚合

    黑群晖挂载USB网卡 一不小心入了群晖的坑 xff0c 然后就是不停的折腾 xff0c 入手一个蜗牛星际C单 xff0c 装好DS918 43 后 xff0c 家里有ASUS RT AC5300 xff0c 就想着搞个链路聚合 首先当然是去
  • CentOS 7 安装zoneminder

    这里写自定义目录标题 CentOS 7 安装zoneminder1 安装centos系统 xff0c 最小化安装2 配置源3 安装 zoneminder 以及环境4 关闭SELinux5 配置时区6 添加php网页支持7 安装配置数据库8
  • linux deploy linux 安装mariadb 无法启动解决办法

    这里写自定义目录标题 linux deploy linux 安装mariadb 无法启动解决办法 linux deploy linux 安装mariadb 无法启动解决办法 手头一个旧手机 xff0c 就想来折腾一番 xff0c 装到数据库
  • linux deploy 安装debian11 armhf中安装Zoneminder

    linux deploy 安装debian11 stable armhf 中安装Zoneminder 折腾了无数遍 xff0c 然后感觉也没啥用 安装linux deploy 手机获取root权限 安装debian xff08 armhf
  • OpenWRT 中创建docker Openwrt和其他宿主机中的docker容器通信

    这里写自定义目录标题 OpenWRT 中使用创建docker Openwrt和其他宿主机中的docker容器通信网络拓扑一 准备1 openwrt镜像 xff0c 2 安装pve 二 PVE配置1 网络配置2 创建docker networ
  • sublime text自定义clang format插件格式化C++代码

    本文内容为在windows平台上通过 sublime text开发自定义插件实现调用clang format对C C 43 43 代码进行格式化 需要安装LLVM xff0c 下载链接 xff1a https github com llvm
  • 四种方法实现UITableView的cell高度自动计算

    UITableview是iOS开发中使用最频繁的一个控件 xff0c 在实际开发中 xff0c 我们经常需要定制cell xff0c 让cell显示图片 文字等 由于cell包含的图片和文字是根据服务器返回的数据进行填充的 xff0c 这就
  • iOS贝塞尔曲线(UIBezierPath)的基本使用方法

    简介 UIBezierPath是对Core Graphics框架的一个封装 xff0c 使用UIBezierPath类我们可以画出圆形 xff08 弧线 xff09 或者多边形 xff08 比如 xff1a 矩形 xff09 等形状 xff
  • 手机termux免root安装kali:一步到位+图形界面

    1 工具 xff1a 安卓 xff08 包括鸿蒙 xff09 手机 WiFi 充足的电量 脑子 2 浏览器搜索termux xff0c vnc viewer xff0c 下载安装 3 对抗华为纯净模式需要一些操作 xff0c 先断网 xff
  • iOS 谓词(NSPredicate)

    文档定义 NSPredicate xff1a A definition of logical conditions used to constrain a search either for a fetch or for in memory
  • 在Ubuntu20.04中安装中文输入法

    引言 在Ubuntu系统中 xff0c 无论是写文档还是在程序中写注释 xff0c 都经常需要用到中文输入法 本文简单介绍了三种输入法框架 xff0c 然后详细介绍了在Ubuntu 20 04系统中 xff0c IBus框架和Fcitx框架
  • LED的C语言应用程序

    引言 在本文中 xff0c 用C语言编写一个LED灯的应用程序 xff0c 对文章 基于HDF的LED驱动程序开发 xff08 1 xff09 xff08 2 xff09 中开发的LED灯的驱动程序进行测试 另外 xff0c 在编写LED灯
  • CodeBlocks 快捷键篇(自己整理)

    浏览最近文件 Ctrl 43 Tab 保存当前文件 Ctrl 43 S 保存所有文件 Ctrl 43 Shift 43 S 关闭当前文件 Ctrl 43 W 关闭所有文件 Ctrl 43 Shift 43 W 跳转到上一个函数 Ctrl 4
  • send的 epoll_wait EPOLLOUT事件 与 MSG_WAITALL参数

    1 send 10G的数据 xff0c send返回值不会是10G xff0c 而是大约256k xff0c 表示你只成功写入了256k的数据 接着调用send xff0c send就会返回EAGAIN xff0c 告诉你socket的缓冲
  • Linux Crontab 设置的定时任务没有启动的排查

    1 先手动执行定时任务以此来判断脚本是否有问题 2 确认服务器是否开启定时任务计划服务 命令 xff1a service crond status service crond span class hljs operator span cl
  • 一键显示电脑IP

    1 新建文本文档 gt 将步骤2的内容存入 2 echo off ipconfig all pause exit 3 另存为 gt 扩展名为bat
  • UITextView 设置行高 并限制字数 并在键盘遮挡时向上移动

    首先添加监听 NSNotificationCenter defaultCenter addObserver self selector 64 selector textFiledEditChanged name 64 34 UITextVi
  • iOS UIAlertController

    运行环境 xff1a Xcode7 2 1 xff0c iOS Simulator9 2 语言 xff1a Objective C Swift 关于UIAlertController的使用 xff0c 主要有三种不同的方式 xff1a 1