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 的用法的更多相关文章

  1. [Swift]UIAlertController 以及 Swift 中的闭包和枚举

    原文地址:http://blog.callmewhy.com/2014/10/08/uialertcontroller-swift-closures-enum/ 在 iOS8 的 SDK 中, UIK ...

  2. swift UIAlertController使用 UIAlertController的宽度 为270

    添加子控件 1. 有标题, alert标题高度大概 是 40, 子控件的 Y一般在40 ,如果中间有换行, \n 的高度大概是30 2.alert的宽度 是270, 设置frame 的时候注意 /// ...

  3. UIAlertController 部分用法及属性

    //创建UIAlertController:初始化UIAlertController 需要使用alertControllerWithTitle UIAlertController *alertCont ...

  4. Swift - enumerateObjectsUsingBlock的用法

    enumerateobjectsusingblock:不是Array的方法在NSArray使用.如果你想使用它,你需要一个实例NSArray而不是Array. import Foundation va ...

  5. swift函数的用法,及其嵌套实例

    import Foundation //swift函数的使用 func sayHello(name userName:String ,age:Int)->String{ return " ...

  6. iOS 9.0中UIAlertController的用法

    UIAlertView和UIActionSheet 被划线了. 苹果不推荐我们使用这两个类了.也不再进行维护和更新 正如苹果所说它现在让我们用UIAlertConntroller 并设置样式为UIAl ...

  7. swift UIAlertController教程

    在iOS8中,UIAlertView与UIActionSheet都已经退休,取而代之的是UIAlertController!它的使用示范如下://弹出一个警告框,标题是“提示”,信息是“我的博客:oa ...

  8. Swift继承的用法

    一个类可以继承另一个类的方法,属性和其它特性.当一个类继承其它类,继承类叫子类,被继承类叫超类(或父类).在Swift中,继承是区分「类」与其它类型的一个基本特征. 在Swift中,类可以调用和访问超 ...

  9. Swift - UITableView的用法

    因为倾向于纯代码编码,所以不太喜欢可视化编程,不过也略有研究,所以项目里面的所有界面效果,全部都是纯代码编写! 终于到了重中之重的tableview的学习了,自我学习ios编程以来,工作中用得最多的就 ...

随机推荐

  1. Linux下crontab内环境变量与Shell环境变量的关系【转】

    crontab,总是不会缺省的从用户profile文件中读取环境变量参数 经常导致在手工执行某个脚本时是成功的,但是到crontab中试图执行时就会报错. 解决办法如下: 方法一:在shell文件中获 ...

  2. LeetCode: Longest Valid Parentheses 解题报告

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  3. [转]好用工具:Oracle SQL Developer

    我們一直以來就很少使用 Oracle 資料庫,一年下來也頂多 1 ~ 2 個案子採用 Oracle 的資料庫,所以一直都對 Oracle 資料庫的操作不太熟悉,尤其是用 Oracle 內建的那些超難用 ...

  4. linux 中常用的一些头文件

    #include <linux/***.h> 是在linux-2.6.29/include/linux下面寻找源文件. #include <asm/***.h> 是在linux ...

  5. Android——列表视图 ListView(一)Arrayadapter

    一.ArrayAdapter 只显示文字 activitylistview_layout.xml <?xml version="1.0" encoding="utf ...

  6. git中文乱码解决方案

    解决方案: 在bash提示符下输入: git config --global core.quotepath false core.quotepath设为false的话,就不会对0x80以上的字符进行q ...

  7. Testng 的数据源 驱动測试 代码与配置

    JUnit中有讲述使用注解的方式进行数据源读取进行自己主动循环測试的方法,在TestNG中也提供了对应的方法 public class TestngDataProvider { /** * 数组内的每 ...

  8. js类型转换 之 转字符串及布尔类型

    上一篇我们讲到了如何转数字类型,今天总结一下转字符串及布尔类型的方法: 转字符串方法主要有: toString(); String(); 具体的用法如下表格所示: 方法 例子 返回值 说明 toStr ...

  9. 【转】WCF入门教程三[WCF的宿主]

    一.WCF服务应用程序与WCF服务库 我们在平时开发的过程中常用的项目类型有“WCF 服务应用程序”和“WCF服务库”. WCF服务应用程序,是一个可以执行的程序,它有独立的进程,WCF服务类契约的定 ...

  10. ADSI Edit 工具

    最近在弄.net的活动目录用到了工具ADSI Edit,网上找了点资料,一来自己记录下,二来分享给大家: 下载的压缩包里存在两个文件adsiedit.dll和adsiedit.msc 1.将adsie ...