IOS

swift之对话框-简单封装

2019-02-22  本文已影响0人  瑟闻风倾

备注:基础使用请参考swift之对话框-基础使用

1. 新建AlertHelper.swift

//
//  AlertHelper.swift
//  JackUChat
//
//  Created by 徐云 on 2019/2/22.
//  Copyright © 2019 Liy. All rights reserved.
//

import Foundation

// MARK: - 创建单例模式
class AlertHelper{
    class var shareInstance: AlertHelper {
        let alertHelper = AlertHelper()
        return alertHelper
    }
    
}

//AlertView适用于仅需提示作用,AlertController适用于需对点击事件进行逻辑处理
extension AlertHelper {
    
     // MARK: - AlertView(已被AlertController替代)
    
    //对话框视图AlertView的使用:默认样式仅有取消一个按钮(适用于仅需提示作用的情况)
    func showAlertViewStyle1(message:String) {
        var alertView = UIAlertView(title: "提示", message: message, delegate: self, cancelButtonTitle: "取消")
        alertView.show()
    }
    
    //对话框视图AlertView的使用:4种自定义样式(适用于仅需提示作用的情况)
    func showAlertViewStyle2(message:String,style:String) {
        var alertView = UIAlertView()
        alertView.delegate = self
        alertView.title = "标题"
        alertView.message = message
        if style == "0" {
            alertView.alertViewStyle = .default
        }else if style == "1" {
            alertView.alertViewStyle = .plainTextInput
        }else if style == "2" {
            alertView.alertViewStyle = .secureTextInput
        }else if style == "3" {
            alertView.alertViewStyle = .loginAndPasswordInput
        }else {
            alertView.alertViewStyle = .default
        }
        
        alertView.addButton(withTitle: "取消")
        alertView.addButton(withTitle: "好的")
        alertView.show()
    }
    
    
    // MARK: - AlertController(对话框样式和上拉菜单样式)
    
    //对话框视图控制器AlertController的使用:默认样式(适用于仅需提示作用的情况)
    func showAlertControllerStyle(currentViewController:UIViewController) {
        //1.对话框样式还是上拉菜单样式
        var alertController = UIAlertController(title: "标题", message: "这个是UIAlertController的默认样式", preferredStyle: UIAlertController.Style.alert)
        //var alertController = UIAlertController(title: "标题", message: "这个是UIAlertController的默认样式", preferredStyle: UIAlertController.Style.actionSheet)
        //2.创建动作按钮并将它们添加到控制器上:通过UIAlertActionStyle来设置三种动作样式:常规(default)、取消(cancel)以及警示(destruective)
        var okAction = UIAlertAction(title: "确定", style: UIAlertAction.Style.default, handler: nil)
        //var resetAction = UIAlertAction(title: "重置", style: UIAlertAction.Style.destructive, handler: nil)
        var cancelAction = UIAlertAction(title: "取消", style: UIAlertAction.Style.cancel, handler: nil)
        alertController.addAction(okAction)
        //alertController.addAction(resetAction)
        alertController.addAction(cancelAction)
        //3.显示对话框视图控制器
        //self.present(alertController, animated: true, completion: nil)
        currentViewController.present(alertController,animated: true,completion: nil)
    }
    
    //对话框视图控制器AlertController的使用:对话框样式封装(适用于需对点击事件进行逻辑处理)
    func showAlertControllerStyle1(currentViewController:UIViewController,message:String,okCompletion:@escaping () -> Void,cancleCompletion:@escaping () -> Void) {
        //1.对话框样式还是上拉菜单样式
        var alertController = UIAlertController(title: "提示", message: message, preferredStyle: UIAlertController.Style.alert)
        //2.创建动作按钮并将它们添加到控制器上:通过UIAlertActionStyle来设置三种动作样式:常规(default)、取消(cancel)以及警示(destruective)
        var okAction = UIAlertAction(title: "确定", style: UIAlertAction.Style.default) {
            (action: UIAlertAction!) -> Void in
            okCompletion()//确认按钮点击的回调
        }
        var resetAction = UIAlertAction(title: "重置", style: UIAlertAction.Style.destructive, handler: nil)
        var cancelAction = UIAlertAction(title: "取消", style: UIAlertAction.Style.cancel) { (UIAlertAction) in
            cancleCompletion()//取消按钮点击的回调
        }
        alertController.addAction(okAction)
        alertController.addAction(resetAction)
        alertController.addAction(cancelAction)
        
        //3.显示对话框视图控制器
        //self.present(alertController, animated: true, completion: nil)
        currentViewController.present(alertController,animated: true,completion: nil)
    }
    
    //对话框视图控制器AlertController的使用:上拉菜单样式封装(适用于需对点击事件进行逻辑处理)
    func showAlertControllerStyle2(currentViewController:UIViewController,message:String,okCompletion:@escaping () -> Void,cancleCompletion:@escaping () -> Void) {
        //1.对话框样式还是上拉菜单样式
        var alertController = UIAlertController(title: "标题", message: message, preferredStyle: UIAlertController.Style.actionSheet)
        //2.创建动作按钮并将它们添加到控制器上:通过UIAlertActionStyle来设置三种动作样式:常规(default)、取消(cancel)以及警示(destruective)
        var okAction = UIAlertAction(title: "确定", style: UIAlertAction.Style.default) {
            (action: UIAlertAction!) -> Void in
            okCompletion()//确认按钮点击的回调
        }
        var resetAction = UIAlertAction(title: "重置", style: UIAlertAction.Style.destructive, handler: nil)
        var cancelAction = UIAlertAction(title: "取消", style: UIAlertAction.Style.cancel) { (UIAlertAction) in
            cancleCompletion()//取消按钮点击的回调
        }
        alertController.addAction(okAction)
        alertController.addAction(resetAction)
        alertController.addAction(cancelAction)
        
        //3.显示对话框视图控制器
        //self.present(alertController, animated: true, completion: nil)
        currentViewController.present(alertController,animated: true,completion: nil)
    }
    
    
}

2. 调用

调用.png

(1)

AlertHelper.shareInstance.showAlertViewStyle1(message: "这个是UIAlertView的默认样式")

(2)

AlertHelper.shareInstance.showAlertViewStyle2(message: "这个是UIAlertView的自定义样式", style: "1")

(3)

AlertHelper.shareInstance.showAlertControllerStyle(currentViewController: self)

(4)

AlertHelper.shareInstance.showAlertControllerStyle1(currentViewController: self, message: "确认操作吗?", okCompletion: {
            print("确认操作的处理")
        }) {
            print("取消操作的处理")
        }   

(5)

AlertHelper.shareInstance.showAlertControllerStyle2(currentViewController: self, message: "确认操作吗?", okCompletion: {
            print("确认操作的处理")
        }) {
            print("取消操作的处理")
        }
上一篇下一篇

猜你喜欢

热点阅读