博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Swift - UIAlertController
阅读量:4977 次
发布时间:2019-06-12

本文共 3822 字,大约阅读时间需要 12 分钟。

let alertView = UIAlertController(title: "系统提示", message: "你确定要退出", preferredStyle: .alert)        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)        let okAction = UIAlertAction(title: "好的", style: .default, handler: {            action in            print("点击了确定")        })        alertView.addAction(cancelAction)        alertView.addAction(okAction)        self.present(alertView, animated: true, completion: nil)

let alertView = UIAlertController(title: "系统提示", message: "你确定要退出", preferredStyle: .actionSheet)        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)        let doNotAction = UIAlertAction(title: "不确定", style: .destructive, handler: nil)        let okAction = UIAlertAction(title: "好的", style: .default, handler: {            action in            print("点击了确定")        })        alertView.addAction(cancelAction)        alertView.addAction(okAction)        alertView.addAction(doNotAction)        self.present(alertView, animated: true, completion: nil)

 

let alertController = UIAlertController(title: "登录", message: "请输入用户名和密码", preferredStyle:.alert)                alertController.addTextField { (textFeild:UITextField!) in            textFeild.placeholder = "用户名"        }        alertController.addTextField { (textFeild:UITextField!) in            textFeild.placeholder = "密码"            textFeild.isSecureTextEntry = true        }                let cancel = UIAlertAction(title: "取消", style:.cancel, handler: nil)        let ok = UIAlertAction(title: "确定", style: .default) { (action:UIAlertAction) in                        let login = alertController.textFields!.first!            let password = alertController.textFields!.last!            print("用户名:\(login.text) 密码:\(password.text)")        }                alertController.addAction(cancel)        alertController.addAction(ok)        self.present(alertController, animated: true, completion: nil)

let alertController = UIAlertController(title: "保存成功", message: nil, preferredStyle: .alert)      self.present(alertController, animated: true, completion: nil)                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {            self.presentedViewController?.dismiss(animated: false, completion: nil)        }

 

UIAlertController扩展(UIAlertExtension.swift)

import UIKit extension UIAlertController {    //在指定视图控制器上弹出普通消息提示框    static func showAlert(message: String, in viewController: UIViewController) {        let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)        alert.addAction(UIAlertAction(title: "确定", style: .cancel))        viewController.present(alert, animated: true)    }         //在根视图控制器上弹出普通消息提示框    static func showAlert(message: String) {        if let vc = UIApplication.shared.keyWindow?.rootViewController {            showAlert(message: message, in: vc)        }    }         //在指定视图控制器上弹出确认框    static func showConfirm(message: String, in viewController: UIViewController,                            confirm: ((UIAlertAction)->Void)?) {        let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)        alert.addAction(UIAlertAction(title: "取消", style: .cancel))        alert.addAction(UIAlertAction(title: "确定", style: .default, handler: confirm))        viewController.present(alert, animated: true)    }         //在根视图控制器上弹出确认框    static func showConfirm(message: String, confirm: ((UIAlertAction)->Void)?) {        if let vc = UIApplication.shared.keyWindow?.rootViewController {            showConfirm(message: message, in: vc, confirm: confirm)        }    }}

 调用

//弹出普通消息提示框UIAlertController.showAlert(message: "保存成功!") //弹出确认选择提示框UIAlertController.showConfirm(message: "是否提交?") { (_) in    print("点击了确认按钮!")}

 

转载于:https://www.cnblogs.com/baidaye/p/9174728.html

你可能感兴趣的文章
BZOJ1026: [SCOI2009]windy数
查看>>
组件:slot插槽
查看>>
Nginx配置文件nginx.conf中文详解(转)
查看>>
POJ 1308 Is It A Tree?(并查集)
查看>>
N进制到M进制的转换问题
查看>>
springIOC第一个课堂案例的实现
查看>>
求输入成绩的平均分
查看>>
php PDO (转载)
查看>>
wordpress自动截取文章摘要代码
查看>>
[置顶] 一名优秀的程序设计师是如何管理知识的?
查看>>
highcharts 图表实例
查看>>
highcharts曲线图
查看>>
extjs动态改变样式
查看>>
宏定义
查看>>
笔记:git基本操作
查看>>
生成php所需要的APNS Service pem证书的步骤
查看>>
JavaWeb之JSON
查看>>
HOT SUMMER 每天都是不一样,积极的去感受生活 C#关闭IE相应的窗口 .
查看>>
optionMenu-普通菜单使用
查看>>
2016-2017-2点集拓扑作业[本科生上课时]讲解视频
查看>>