Latest 1.0.0
Homepage https://github.com/boai/BAAlertController-Swift
License MIT
Platforms ios 9.0, requires ARC
Authors

BAHome Team Name


1、功能及简介

  • 1、用分类封装系统的 UIAlertController,代码无侵入,简洁高效,一个block 搞定系统alert 和 actionSheet
  • 2、可以自定义按钮数量、按钮颜色【多按钮样式,颜色数组和title数组个数不等情况下,默认蓝色】【待开发】
  • 3、可以自定义 NSMutableAttributedString 的 alert 和 actionSheet,让你的 alert 更加炫酷【待开发】
  • 4、完美适配 iPhone 和 iPad 版本
  • 5、理论完全兼容现有 iOS 8 以上系统版本

2、图片示例

BAAlertController1
BAAlertController2
BAAlertController3
BAAlertController4
BAAlertController-5
BAAlertController6
BAAlertController7

3、安装、导入示例和源码地址

  • 1、pod 导入【最新版本:】:
    OC: pod 'BAAlertController'
    Swift: pod 'BAAlertController-Swift'

如果发现 pod search BAAlertController-Swift 搜索出来的不是最新版本,需要在终端执行 cd 转换文件路径命令退回到 desktop,然后执行 pod setup 命令更新本地spec缓存(可能需要几分钟),然后再搜索就可以了。
具体步骤:

4、BAAlertController-Swift 的类结构及 demo 示例

BAAlertController-Swift

UIAlertController+BAKit.swift

import Foundation
import UIKit

public typealias BAKit_UIAlertController_ButtonActionBlock = (_ index:Int, _ alertController:UIAlertController) -> ()
public typealias BAKit_UIAlertController_TextFieldConfigurationActionBlock = (_ textField:UITextField, _ index:Int) -> ()

extension UIAlertController {

    /// UIAlertController:快速创建一个 普通的 alert
    ///
    /// - Parameters:
    ///   - viewController: viewController
    ///   - title: 标题
    ///   - message: 内容
    ///   - buttonTitleArray: 按钮标题数组
    ///   - buttonTitleColorArray: 按钮标题颜色数组
    ///   - actionBlock: 按钮点击事件 block
    public func ba_alertController(_ viewController: UIViewController, title: String,
                                   message: String, buttonTitleArray:Array<String?>,
                                   buttonTitleColorArray:Array<UIColor?>, actionBlock : @escaping BAKit_UIAlertController_ButtonActionBlock) {

        self.ba_creatAlertController(UIAlertControllerStyle.alert, viewController: viewController, title: title, message: message, buttonTitleArray: buttonTitleArray, buttonTitleColorArray: buttonTitleColorArray, buttonDisabledWithTitleArray: [], textFieldPlaceholderArray: [], textFieldConfigurationActionBlock: nil , actionBlock: actionBlock)
    }

    /// UIAlertController:快速创建一个 普通的 actionSheet
    ///
    /// - Parameters:
    ///   - viewController: viewController
    ///   - title: 标题
    ///   - message: 内容
    ///   - buttonTitleArray: 按钮标题数组
    ///   - buttonTitleColorArray: 按钮标题颜色数组
    ///   - actionBlock: 按钮点击事件 block
    public func ba_actionSheet(_ viewController: UIViewController, title: String,
                                   message: String, buttonTitleArray:Array<String?>,
                                   buttonTitleColorArray:Array<UIColor?>, actionBlock : @escaping BAKit_UIAlertController_ButtonActionBlock) {

        self.ba_creatAlertController(UIAlertControllerStyle.actionSheet, viewController: viewController, title: title, message: message, buttonTitleArray: buttonTitleArray, buttonTitleColorArray: buttonTitleColorArray, buttonDisabledWithTitleArray: [], textFieldPlaceholderArray: [], textFieldConfigurationActionBlock: nil , actionBlock: actionBlock)
    }

    /// UIAlertController: 快速创建一个系统 普通 带 textField 的 alert
    ///
    /// - Parameters:
    ///   - viewController: viewController
    ///   - title: 标题
    ///   - message: 内容
    ///   - buttonTitleArray: 按钮标题数组
    ///   - buttonTitleColorArray: 按钮标题颜色数组
    ///   - buttonDisabledWithTitleArray: 按钮标题颜色数组(默认不能点击)
    ///   - textFieldPlaceholderArray: textField 的 Placeholder 数组
    ///   - textFieldConfigurationActionBlock: textField 配置 block
    ///   - actionBlock: 按钮点击事件 block
    public func ba_alertController(_ viewController: UIViewController, title: String,
                                   message: String, buttonTitleArray:Array<String?>,
                                   buttonTitleColorArray:Array<UIColor?>, buttonDisabledWithTitleArray:Array<String?>,
                                   textFieldPlaceholderArray:Array<String?>,
                                   textFieldConfigurationActionBlock: BAKit_UIAlertController_TextFieldConfigurationActionBlock?, actionBlock : @escaping BAKit_UIAlertController_ButtonActionBlock) {

        self.ba_creatAlertController(UIAlertControllerStyle.alert, viewController: viewController, title: title, message: message, buttonTitleArray: buttonTitleArray, buttonTitleColorArray: buttonTitleColorArray, buttonDisabledWithTitleArray: buttonDisabledWithTitleArray, textFieldPlaceholderArray: textFieldPlaceholderArray, textFieldConfigurationActionBlock: textFieldConfigurationActionBlock, actionBlock: actionBlock)
    }

    private func ba_creatAlertController(_ preferredStyle:UIAlertControllerStyle,
                                         viewController: UIViewController,
                                         title: String,
                                         message: String,
                                         buttonTitleArray:Array<String?>,
                                         buttonTitleColorArray:Array<UIColor?>,
                                         buttonDisabledWithTitleArray:Array<String?>,
                                         textFieldPlaceholderArray:Array<String?>,
                                         textFieldConfigurationActionBlock: BAKit_UIAlertController_TextFieldConfigurationActionBlock?,
                                         actionBlock : BAKit_UIAlertController_ButtonActionBlock?) {

        let alertController = UIAlertController.init(title: title, message: message, preferredStyle: preferredStyle)

        if buttonTitleArray.count > 0
        {
            for i:Int in 0..<buttonTitleArray.count
            {
                let buttonTitle = buttonTitleArray[i]

                let action = UIAlertAction.init(title: buttonTitle, style: UIAlertActionStyle.default, handler: { (action) in
                    actionBlock!(i, alertController)
                })
                alertController.addAction(action)

                for j:Int in 0..<buttonDisabledWithTitleArray.count
                {
                    if (buttonDisabledWithTitleArray[j]?.elementsEqual(buttonTitle!))!
                    {
                        action.isEnabled = false
                    }
                }
                // 处理按钮字体颜色,待后期版本更新

            }
        }

        if preferredStyle == UIAlertControllerStyle.alert && textFieldPlaceholderArray.count > 0
        {
            for i:Int in 0..<textFieldPlaceholderArray.count
            {
                alertController.addTextField(configurationHandler: { (textField) in
                    textField.placeholder = textFieldPlaceholderArray[i]

                    textFieldConfigurationActionBlock!(textField, i)
                })
            }
        }

        if preferredStyle == UIAlertControllerStyle.actionSheet
        {
            let action = UIAlertAction.init(title: "取消", style: UIAlertActionStyle.cancel, handler: nil)
            alertController.addAction(action)
        }

        viewController.ba_currentViewController()?.present(alertController, animated: true, completion: nil)

    }
}

extension UIViewController {
    public func ba_currentViewController() -> UIViewController? {

        var presentedVC = UIApplication.shared.keyWindow?.rootViewController
        while let pVC = presentedVC?.presentedViewController
        {
            presentedVC = pVC
        }

        if presentedVC == nil {
            print("UIAlertController Error: You don't have any views set. You may be calling in viewDidload. Try viewDidappear.")
        }
        return presentedVC
    }
}

demo 示例

    func ba_alert()  {
        UIAlertController().ba_alertController(self, title: title0, message: msg0, buttonTitleArray: buttonTitleArray, buttonTitleColorArray: []) { (index, alertController) in

            print("点我干嘛?index:", index)
        }
    }

    func ba_alert2() -> Void {
        UIAlertController().ba_actionSheet(self, title: title0, message: msg0, buttonTitleArray: buttonTitleArray, buttonTitleColorArray: []) { (index, alertController) in
            print("点我干嘛?index:", index)
        }
    }

    func ba_alert3() -> Void {
        UIAlertController().ba_alertController(self, title: title0, message: msg0, buttonTitleArray: buttonTitleArray, buttonTitleColorArray: [], buttonDisabledWithTitleArray: ["确定"], textFieldPlaceholderArray: ["请输入账号", "请输入密码"], textFieldConfigurationActionBlock: { (textField, index) in

            if index == 0
            {
                textField.keyboardType = .numberPad
            }
            if index == 1
            {
                textField.isSecureTextEntry = true
            }

            NotificationCenter.default.addObserver(self, selector: #selector(self.handleAlertTextFieldDidChangeAction(_ :)), name: NSNotification.Name.UITextFieldTextDidChange, object: textField)

        }) { (index, alertController) in

        }
    }

    @objc func handleAlertTextFieldDidChangeAction(_ notification: Notification) -> Void {
        // 通知处理,判断文字输入的长度 大于 3 的时候,确定按钮可点击,否则,不可点击
        let alertController:UIAlertController = self.presentedViewController as! UIAlertController;

        let login:UITextField = alertController.textFields![0];
        let sureAction:UIAlertAction = alertController.actions[1];

        let loginString:String = login.text!
        sureAction.isEnabled = loginString.count > 3;
    }

其他示例可下载demo查看源码!

5、更新记录:【倒叙】

欢迎使用 【BAHome】 系列开源代码 !
如有更多需求,请前往:【BAHome】

项目源码地址:
OC 版 :https://github.com/BAHome/BAAlertController
Swift 版 :https://github.com/BAHome/BAAlertController-Swift

最新更新时间:2018-01-06 【倒叙】
最新Version:【Version:1.0.0】
更新内容:
1.0.0.1、普通的 UIAlertController-alert,可以自由设置按钮数量、按钮颜色
1.0.0.2、普通的 UIAlertController-alert,可以自由设置按钮数量、按钮颜色【多按钮样式,颜色数组和title数组个数不等】
1.0.0.3、普通的 带 textField 的 UIAlertController-alert,可添加一个或者多个 textField
1.0.0.4、普通的 UIAlertController-actionSheet,可以自由设置按钮数量、按钮颜色
1.0.0.5、完美适配 iPhone 和 iPad 版本
1.0.0.6、理论完全兼容现有 iOS 8 以上系统版本

6、bug 反馈

1、开发中遇到 bug,希望小伙伴儿们能够及时反馈与我们 BAHome 团队,我们必定会认真对待每一个问题!

2、以后提需求和 bug 的同学,记得把 git 或者博客链接给我们,我直接超链到你们那里!希望大家积极参与测试!

7、BAHome 团队成员

1、QQ 群
479663605
【注意:此群为 2 元 付费群,介意的小伙伴儿勿扰!】

孙博岩
QQ:137361770
git:https://github.com/boai
简书:http://www.jianshu.com/u/95c9800fdf47
微博:

马景丽
QQ:1253540493
git:https://github.com/MaJingli

陆晓峰
QQ:442171865
git:https://github.com/zeR0Lu

陈集
QQ:3161182978
git:https://github.com/chenjipdc
简书:http://www.jianshu.com/u/90ae559fc21d

任子丰
QQ:459643690
git:https://github.com/renzifeng

吴丰收
QQ:498121294

石少庸
QQ:363605775
git:https://github.com/CrazyCoderShi
简书:http://www.jianshu.com/u/0726f4d689a3

8、开发环境 和 支持版本

开发使用 最新版本 Xcode,理论上支持 iOS 9 及以上版本,如有版本适配问题,请及时反馈!多谢合作!

9、感谢

感谢 【BAHome】 团队成员倾力合作,后期会推出一系列 常用 UI 控件的封装,大家有需求得也可以在 issue 提出,如果合理,我们会尽快推出新版本!

【BAHome】 的发展离不开小伙伴儿的信任与推广,再次感谢各位小伙伴儿的支持!

Latest podspec

{
    "name": "BAAlertController-Swift",
    "version": "1.0.0",
    "summary": "UIAlertController u7684u5206u7c7buff0cu4e00u4e2a block u641eu5b9au7cfbu7edf alert u548c actionSheet u7684 iPhone u548c iPad u7248u672cu9002u914duff01",
    "homepage": "https://github.com/boai/BAAlertController-Swift",
    "license": "MIT",
    "authors": {
        "boai": "[email protected]"
    },
    "platforms": {
        "ios": "9.0"
    },
    "source": {
        "git": "https://github.com/boai/BAAlertController-Swift.git",
        "tag": "1.0.0"
    },
    "source_files": "BAAlertController-Swift/BAAlertController-Swift/*.{swift}",
    "requires_arc": true
}

Pin It on Pinterest

Share This