Swift - UIAlertController基本用法

  func testAlert(){
        let alertController = UIAlertController(title: "提示", message: "确定要离开该页面吗?", preferredStyle: .alert)
        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil);
        let okAction = UIAlertAction(title: "确定", style: .default) { (action) in
            print("点击了确定")
        }
        alertController.addAction(cancelAction)
        alertController.addAction(okAction)
        self.present(alertController, animated: true, completion: nil)
    }

这里写图片描述

   func testSheet(){
        let alertController = UIAlertController(title: "保存或删除数据", message: "删除数据后无法恢复", preferredStyle: .actionSheet)
        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        let saveAction = UIAlertAction(title: "保存", style: .default, handler: nil)
        let deleteAction = UIAlertAction(title: "删除", style: .destructive, handler: nil)
        alertController.addAction(cancelAction)
        alertController.addAction(saveAction)
        alertController.addAction(deleteAction)
        self.present(alertController, animated: true, completion: nil)
    }

这里写图片描述

   func testTextFieldAlert(){
       let alertController = UIAlertController(title: "新建文件夹", message: "请输入文件夹名称", preferredStyle: .alert)
        alertController.addTextField { (textField) in
            textField.placeholder = "新建文件夹名称"
        }
       let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
       let okAction = UIAlertAction(title: "好的", style: .default, handler: nil)
        alertController.addAction(cancelAction)
        alertController.addAction(okAction)
        self.present(alertController, animated: true, completion: nil)

    }

这里写图片描述

func testMessage(){
        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: true, 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, handler: nil))
        viewController.present(alert, animated: true, completion: nil)
    }


    // 在根视图控制器上弹出普通消息提示框
    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:@escaping (UIAlertAction) -> Void){
        let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
        alert.addAction(UIAlertAction(title: "确定", style: .default, handler: confirm))
        viewController.present(alert, animated: true, completion: nil)
    }

    // 在根视图控制器上弹出确认框
    static func showConfirm(message:String, confirm:@escaping (UIAlertAction) -> Void){
        if let vc = UIApplication.shared.keyWindow?.rootViewController {
            showConfirm(message: message, in: vc, confirm: confirm)
        }
    }
}

调用方法
UIAlertController.showConfirm(message: “保存数据”, in: self) { (action) in
print(“点击了确认”)
}
参考链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值