欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ios-UIAlertController(弹框)

程序员文章站 2022-07-02 14:26:23
...

第一种弹框

ios-UIAlertController(弹框)

//设置弹框主体
let alertController = UIAlertController(title: "温馨提示", message: "你确定不给我钱吗?", preferredStyle: .alert)
//设置取消按钮,以及监听事件
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: {
            ACTION in
            print("点击了取消")
        })
//设置确定按钮,及其监听事件
let okAction = UIAlertAction(title: "确定", style: .default, handler: {
            ACTION in
            print("点击了确定")
        })
//将设置的按钮添加到 alertController中
alertController.addAction(cancelAction)
alertController.addAction(okAction)
//将alertController添加到 当前(self)中
self.present(alertController, animated: true, completion: nil)

第二种

ios-UIAlertController(弹框)

//创建弹框主题,设置弹出样式为  actionSheet
let alertController = UIAlertController(title: "操作", message: "请选择", preferredStyle: .actionSheet)
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler:{
            ACTION in
            print("取消")
        })
let deletAction = UIAlertAction(title: "删除", style: .destructive, handler: {
            ACTION in
            print("删除")
        })
let insertAction = UIAlertAction(title: "保存", style: .default, handler: {
            ACTION in
            print("保存")
        })
//添加 alertController中
alertController.addAction(cancelAction)
alertController.addAction(deletAction)
alertController.addAction(insertAction)
self.present(alertController, animated: true, completion: nil)

第三种

ios-UIAlertController(弹框)

//创建有输入框的弹框
let alertController = UIAlertController(title: "系统登录", message: "请输入用户和密码", preferredStyle: .alert)
//设置输入框
alertController.addTextField { (textField: UITextField!) in
            textField.placeholder = "用户名"
        }
//设置输入框
alertController.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 = alertController.textFields![0]
            //获取第二个文本框
            let password = alertController.textFields![1]
            print("用户名:\(String(describing: login.text)) 密码:\(String(describing: password.text))")
        })
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)

第四种

ios-UIAlertController(弹框)

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扩展(方便调用)

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("点击了确认按钮!")
}
相关标签: ios 弹框