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)

第二种

这里写图片描述

//创建弹框主题,设置弹出样式为  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)

第三种

这里写图片描述

//创建有输入框的弹框
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)

第四种

这里写图片描述

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("点击了确认按钮!")
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值