alert封装 swift
2018-09-26 14:33:00 weixin_34208283 阅读数 2

处理 取消按钮 + N个按钮 的点击事件

支持 alert 和 actionSheet 两种样式

封装代码

import UIKit

class BYAlertActionSheetTool { // 处理 取消按钮 + N个按钮 的点击事件
    static func showAlert(titleStr: String?, msgStr: String?, style: UIAlertController.Style = .alert, currentVC: UIViewController, cancelBtn: String = "取消", cancelHandler:((UIAlertAction) -> Void)?, otherBtns:Array<String>?, otherHandler:((Int) -> ())?) {
        //DispatchQueue.global().async{}//子线程
        DispatchQueue.main.async { // 主线程执行
            let alertController = UIAlertController(title: titleStr, message: msgStr,preferredStyle: style)
            //取消按钮
            let cancelAction = UIAlertAction(title:cancelBtn, style: .cancel, handler:{ (action) -> Void in
                cancelHandler?(action)
            })
            alertController.addAction(cancelAction)
            //其他按钮
            if otherBtns != nil {
                for (index, value) in (otherBtns?.enumerated())! {
                    let otherAction = UIAlertAction(title: value, style: .default, handler: { (action) in
                        otherHandler!(index)
                    })
                    alertController.addAction(otherAction)
                }
            }
             currentVC.present(alertController, animated: true, completion: nil)
        }
    }
}

调用代码

BYAlertActionSheetTool.showAlert(titleStr: nil, msgStr: "电话号码不能为空,是否删除该联络人?", style: .actionSheet, currentVC: self, cancelBtn: "取消", cancelHandler: { (cancelAction) in
                printLog(message: "点击了取消按钮")
                
            }, otherBtns: ["拍照","相册","录像","小视频"]) { (idx) in
                printLog(message: "点击了第\(idx)个按钮")
                
            }

极简调用(默认alert样式,“取消”)

BYAlertActionSheetTool.showAlert(titleStr: nil, msgStr: "电话号码不能为空,是否删除该联络人?", currentVC: self, cancelHandler: nil, otherBtns: nil, otherHandler: nil)

效果图

4988812-ee7c029cf0789832.png
屏幕快照 2018-09-26 下午2.25.44.png
4988812-91fe986ee975a441.png
屏幕快照 2018-09-26 下午2.26.23.png
4988812-5047997c51f6a1ed.png
屏幕快照 2018-09-26 下午2.33.05.png
2017-10-24 14:19:00 weixin_34008805 阅读数 39
let title: String = "您的开奖时间为"
let time: String = "2017-10-23 12:23:18"
let count_down: NSString = "6" as NSString
var countdown = count_down.integerValue
let timeout: String = "开奖时间已超时,请重新获取"
let alertVc = UIAlertController.init(title: nil, message: title + "\n" + time, preferredStyle: UIAlertControllerStyle.alert)
let alertAction0 = UIAlertAction.init(title: "取消", style: .default, handler: { (action) in
    
})
let alertAction1 = UIAlertAction.init(title: "确定(\(countdown))", style: .default, handler: { (action) in
    //确定的操作
})
alertVc.addAction(alertAction1)
alertVc.addAction(alertAction0)
weakSelf?.present(alertVc, animated: true, completion: {
})
if countdown != 0 {
    let queue: DispatchQueue = DispatchQueue.global()
    let countdownTimer = DispatchSource.makeTimerSource(flags: [], queue: queue)
    countdown = countdown + 1
    countdownTimer.scheduleRepeating(deadline: .now(), interval: .seconds(1))
    countdownTimer.setEventHandler(handler: {
        countdown = countdown - 1
        if countdown <= 0 {
            countdownTimer.cancel()
            DispatchQueue.main.async {
                alertAction1.setValue("确定(0)", forKey: "title")
                alertAction1.setValue(UIColor.hrgb("CCCCCC"), forKey: "titleTextColor")
                alertAction1.isEnabled = false
                // message
                let one: NSString = "\(title)\n\(time)\n" as NSString
                let two: NSString = "\(timeout)" as NSString
                let message = "\(title)\n\(time)\n\(timeout)"
                let alertControllerMessageStr = NSMutableAttributedString(string: message)
                alertControllerMessageStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.hrgb("DF503C"), range: NSMakeRange(one.length, two.length))
                alertControllerMessageStr.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 13), range: NSMakeRange(one.length, two.length))
                alertVc.setValue(alertControllerMessageStr, forKey: "attributedMessage")
            }
        }else {
            DispatchQueue.main.async {
                alertAction1.setValue("确定(\(countdown))", forKey: "title")
            }
        }
    })
    countdownTimer.resume()
}
2110475-a986bd2039c8a744.png
0.png
2110475-fbfe5a07aa6017ae.png
1.png
2016-07-28 17:31:03 u011213403 阅读数 1773

转载自   http://www.hangge.com/blog/cache/detail_651.html

1,简单的应用(同时按钮响应Handler使用闭包函数)
  
原文:Swift - 告警提示框(UIAlertController)的用法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import UIKit
 
class ViewControllerUIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
     
    override func viewDidAppear(animated: Bool){
        super.viewDidAppear(animated)
         
        let alertController = UIAlertController(title: "系统提示",
            message: "您确定要离开hangge.com吗?", preferredStyle: .Alert)
        let cancelAction = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
        let okAction = UIAlertAction(title: "好的", style: .Default,
            handler: {
                action in
                print("点击了确定")
        })
        alertController.addAction(cancelAction)
        alertController.addAction(okAction)
        self.presentViewController(alertController, animated: true, completion: nil)
    }
     
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

2,除了弹出,还可以使用从底部向上滑出的样式
(注意:如果上拉菜单中有“取消”按钮的话,那么它永远都会出现在菜单的底部,不管添加的次序是如何)
 
原文:Swift - 告警提示框(UIAlertController)的用法
1
2
3
4
5
6
7
8
9
let alertController = UIAlertController(title: "保存或删除数据", message: "删除数据将不可恢复",
    preferredStyle: .ActionSheet)
let cancelAction = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
let deleteAction = UIAlertAction(title: "删除", style: .Destructive, handler: nil)
let archiveAction = UIAlertAction(title: "保存", style: .Default, handler: nil)
alertController.addAction(cancelAction)
alertController.addAction(deleteAction)
alertController.addAction(archiveAction)
self.presentViewController(alertController, animated: true, completion: nil)

3,按钮使用“告警”样式(文字颜色变红,用来来警示用户)
  
原文:Swift - 告警提示框(UIAlertController)的用法
1
var okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.Destructive, handler:nil)

4,添加任意数量文本输入框(比如可以用来实现个登陆框)
  
原文:Swift - 告警提示框(UIAlertController)的用法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import UIKit
 
class ViewControllerUIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
     
    override func viewDidAppear(animated: Bool){
        super.viewDidAppear(animated)
         
        let alertController = UIAlertController(title: "系统登录",
            message: "请输入用户名和密码", preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addTextFieldWithConfigurationHandler {
            (textField: UITextField!) -> Void in
            textField.placeholder = "用户名"
        }
        alertController.addTextFieldWithConfigurationHandler {
            (textField: UITextField!) -> Void in
            textField.placeholder = "密码"
            textField.secureTextEntry = true
        }
        let cancelAction = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
        let okAction = UIAlertAction(title: "好的", style: .Default,
            handler: {
                action in
                //也可以用下标的形式获取textField let login = alertController.textFields![0]
                let login = alertController.textFields!.first! as UITextField
                let password = alertController.textFields!.last! as UITextField
                print("用户名:\(login.text) 密码:\(password.text)")
        })
        alertController.addAction(cancelAction)
        alertController.addAction(okAction)
        self.presentViewController(alertController, animated: true, completion: nil)
    }
     
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

5,使用代码移除提示框
1
self.presentedViewController?.dismissViewControllerAnimated(false, completion: nil)

2017-01-09 10:57:30 Xoxo_x 阅读数 197

let alertController = UIAlertController(title: “提示”, message: “确定修改为默认地址?”, preferredStyle:.Alert)

        let cancelAction = UIAlertAction(title: "取消", style:.Cancel , handler: nil)
        let okAction = UIAlertAction(title: "确定", style: .Default, handler: {
            action in

            saveReceivingSpace(self.invoiceList[bt.tag], successHandler: { (state) in

                }, failHandler: { (err) in

            })
        })
        alertController.addAction(cancelAction)
        alertController.addAction(okAction)
        self.presentViewController(alertController, animated: true, completion: nil)
2017-07-09 01:48:00 weixin_34115824 阅读数 3
@IBAction func change(_ sender: Any) {

            let deepAlert2 = UIAlertController(title: "不保存", message: "不保存刚刚的修改吗", preferredStyle: .alert)
            let yesAction = UIAlertAction(title: "不保存", style: .default, handler: { (action:UIAlertAction) in
                self.presentedViewController?.dismiss(animated: true, completion: nil)
                self.navigationController?.popViewController(animated: true)
                // 我这里是直接通过popViewController来进行返回上一层的操作的。因为用了这个changeBtn来覆盖掉原来NavigationController自带的那个返回按钮。
            })
            let noAction = UIAlertAction(title: "返回继续编辑", style: .default, handler: { (action:UIAlertAction) in
                self.presentedViewController?.dismiss(animated: true, completion: nil)
                // do nothing
            })
            deepAlert2.addAction(yesAction)
            deepAlert2.addAction(noAction)
            // self.presentedViewController?.dismiss(animated: true, completion: nil)
            self.present(deepAlert2, animated: true, completion: nil)
 
    }

转载于:https://www.jianshu.com/p/27ccb0b564fd

Swift 封装

阅读数 78

Swift 封装

阅读数 25

Swift显示alert和prompconfirmation

博文 来自: csharp25

Swift Alert弹框提示

阅读数 1532

swift 通知的封装

阅读数 324

没有更多推荐了,返回首页