swift5学习之旅之UIAlertController

27 篇文章 0 订阅

swift5学习之旅-----UIAlertController

三种类型的Alert
在这里插入图片描述

  • 整体代码
    Github还没上传,先用着Dropbox(可能要翻墙),看完有收获的感谢点个赞👍,如果翻不了墙要代码可以私聊我
    https://www.dropbox.com/sh/vp94ctk73frp97t/AABuyZ47DpyjALWjcNjRk480a?dl=0

Dropbox AlertController整体代码

  • UIAlertController基本使用方法
 let alertController = UIAlertController(title: "登录界面", message: "请输入用户名和密码", preferredStyle: .alert)
alertController.addTextField { (textField) in
            textField.placeholder = "请输入用户名"
        }
        
        alertController.addTextField { (textField) in
            textField.placeholder = "密码"
            textField.isSecureTextEntry = true
        }
        let okAction = UIAlertAction(title: "确认", style: .default) { (action) in
            let login = self.alertController.textFields!.first!
            let password = self.alertController.textFields!.last!
            print("用户名:\(String(describing: login.text))密码:\(String(describing: password.text))")
        }
        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        let unknow = UIAlertAction(title: "中间且红色", style: .destructive) { (UIAlertAction) in
            print("红色")
        }
        alertController.addAction(cancelAction)
        alertController.addAction(okAction)
        alertController.addAction(unknow)
  • Extension UIAlertController
extension UIAlertController {
    //在指定视图控制器上弹出普通消息提示框
    static func showAlert(message: String, in viewController: UIViewController) {
        let alert = UIAlertController(title: "Extension Alert", 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.windows.filter({$0.isKeyWindow}).first?.rootViewController  {
            showAlert(message: message, in: vc)
        }
    }
     
    //在指定视图控制器上弹出确认框
    static func showConfirm(message: String, in viewController: UIViewController,
                            confirm: ((UIAlertAction)->Void)?) {
        let alert = UIAlertController(title: "Extension Alert", 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.windows.filter({$0.isKeyWindow}).first?.rootViewController {
            showConfirm(message: message, in: vc, confirm: confirm)
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值