swift - UIAlertController 的用法
ios 8 以后苹果官方建议使用UIAlertController这个类,所以专门去网上找资料,了解了下用法,
1、创建一个alertController
let alertController = UIAlertController(title: "系统提示",
message: "您确定要离开吗?", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
let okAction = UIAlertAction(title: "好的", style: .default,
handler: {
action in
print("点击了确定")
})
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: {
//这里可以做一些其他操作
})
2、创建一个actionSheet
(注:如果上拉菜单中有“取消”按钮的话,那么它永远都会出现在菜单的底部,不管添加的次序是如何)
let alertController = UIAlertController(title: "保存或删除数据", message: "删除数据将不可恢复",
preferredStyle: .actionSheet)
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
let deleteAction = UIAlertAction(title: "删除", style: .destructive, handler: nil)
let archiveAction = UIAlertAction(title: "保存", style: .default, handler: nil)
alertController.addAction(cancelAction)
alertController.addAction(deleteAction)
alertController.addAction(archiveAction)
self.present(alertController, animated: true, completion: nil)
3、按钮使用警告模式,文字颜色变化,用来警示用户
var okAction = UIAlertAction(title: "好的", style: .destructive, handler: nil)
4、添加任意数量的文本输入框
let myAlertController = UIAlertController(title:"系统登录",message:"请输入用户名和密码",preferredStyle:.alert)
myAlertController.addTextField { (textField:UITextField) in
textField.placeholder = "用户名"
} myAlertController.addTextField { (textField:UITextField) in
textField.placeholder = "密码"
textField.isSecureTextEntry = true
} myAlertController.addTextField { (textField:UITextField) in
textField.placeholder = "重复密码"
textField.isSecureTextEntry = true
} let cancelAction = UIAlertAction(title:"取消",style:.cancel,handler:nil)
let okAction = UIAlertAction(title:"确定",style:.default,
handler:{
action in
let login = myAlertController.textFields?.first
let passWord = myAlertController.textFields?.last
print("用户名是:\(String(describing: login)) 密码是:\(String(describing: passWord))")
}) myAlertController.addAction(cancelAction)
myAlertController.addAction(okAction)
self.present(myAlertController, animated: true) {
self.view.backgroundColor = UIColor.blue
}
如图
5、使用代码移除提示框
self.dismiss(animated: true) {
//其他操作
}
swift - UIAlertController 的用法的更多相关文章
- [Swift]UIAlertController 以及 Swift 中的闭包和枚举
原文地址:http://blog.callmewhy.com/2014/10/08/uialertcontroller-swift-closures-enum/ 在 iOS8 的 SDK 中, UIK ...
- swift UIAlertController使用 UIAlertController的宽度 为270
添加子控件 1. 有标题, alert标题高度大概 是 40, 子控件的 Y一般在40 ,如果中间有换行, \n 的高度大概是30 2.alert的宽度 是270, 设置frame 的时候注意 /// ...
- UIAlertController 部分用法及属性
//创建UIAlertController:初始化UIAlertController 需要使用alertControllerWithTitle UIAlertController *alertCont ...
- Swift - enumerateObjectsUsingBlock的用法
enumerateobjectsusingblock:不是Array的方法在NSArray使用.如果你想使用它,你需要一个实例NSArray而不是Array. import Foundation va ...
- swift函数的用法,及其嵌套实例
import Foundation //swift函数的使用 func sayHello(name userName:String ,age:Int)->String{ return " ...
- iOS 9.0中UIAlertController的用法
UIAlertView和UIActionSheet 被划线了. 苹果不推荐我们使用这两个类了.也不再进行维护和更新 正如苹果所说它现在让我们用UIAlertConntroller 并设置样式为UIAl ...
- swift UIAlertController教程
在iOS8中,UIAlertView与UIActionSheet都已经退休,取而代之的是UIAlertController!它的使用示范如下://弹出一个警告框,标题是“提示”,信息是“我的博客:oa ...
- Swift继承的用法
一个类可以继承另一个类的方法,属性和其它特性.当一个类继承其它类,继承类叫子类,被继承类叫超类(或父类).在Swift中,继承是区分「类」与其它类型的一个基本特征. 在Swift中,类可以调用和访问超 ...
- Swift - UITableView的用法
因为倾向于纯代码编码,所以不太喜欢可视化编程,不过也略有研究,所以项目里面的所有界面效果,全部都是纯代码编写! 终于到了重中之重的tableview的学习了,自我学习ios编程以来,工作中用得最多的就 ...
随机推荐
- 使用SimpleMDE富文本编辑器
1). 安装 SimpleMDE npm install simplemde --save 2). 使用 SimpleMDE 1.打开 src/views/articles/Create.vue 文件 ...
- .net core相关博客
http://www.cnblogs.com/artech/蒋金楠,网名Artech,知名IT博主, 微软多领域MVP,畅销IT图书作者,著<WCF全面解析>.<ASP.NET MV ...
- MySQL的使用笔记
@Reference Manual (以下简称REF-M) 在官网,选择MySQL Server,选择Documentation,就可以下载了,建议下载PDF 注意版本要对应 很重要,需要的时候就go ...
- LeetCode: Max Points on a Line 解题报告
Max Points on a Line Given n points on a 2D plane, find the maximum number of points that lie on the ...
- Leetcode: LRU Cache 解题报告
LRU Cache Design and implement a data structure for Least Recently Used (LRU) cache. It should supp ...
- lua——string之string.gsub
translated from the lua document string.gsub用法: 函数原型:string.gsub( s, pattern, rep1[, n] ) 函数功能:返回一个和 ...
- java-结合c3p0封装的db 事务 类
将Connection对象,绑定到当前线程中去,这样在每一个方法中都能使用这个链接. DataSourceUtils.java package com.itheima.utils; import ja ...
- 06 Locking and Latching
本章提要---------------------------------------------------------------6,7,8,9,10,11 这 6 章要细看, 从本章开始how ...
- C#分割文件内容
static void ReadData(string sourcePath, string targetDirectory) { FileStream fs = new FileStream(sou ...
- 【这特么是个坑。。。】iOS 10.3下解决Charles抓包ssl证书信任问题
针对近期iOS 10.3以上的系统charles抓https信任问题 前言 最近iPhone系统更新到ios 10.3后,在公司里用Charles抓包竟然出现了一些问题,https的请求都会失败,提示 ...