Extension+UIAlertController.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // Extension+UIAlertController.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2018/9/13.
  6. // Copyright © 2018年 南鑫林. All rights reserved.
  7. //
  8. import UIKit
  9. extension UIAlertController {
  10. /// 在指定视图控制器上弹出普通消息提示框2消失
  11. static func showAlert(title: String, in viewController: UIViewController) {
  12. let alert = UIAlertController(title: title, message: nil, preferredStyle: .alert)
  13. viewController.present(alert, animated: true)
  14. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
  15. viewController.presentedViewController?.dismiss(animated: false, completion: nil)
  16. }
  17. }
  18. ///在根视图控制器上弹出普通消息提示框2s消失
  19. static func showAlert(title: String) {
  20. if let vc = UIApplication.shared.keyWindow?.rootViewController {
  21. showAlert(title: title, in: vc)
  22. }
  23. }
  24. ///在指定视图控制器上弹出普通消息提示框
  25. static func showAlert(message: String, in viewController: UIViewController) {
  26. let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
  27. alert.addAction(UIAlertAction(title: "确定", style: .cancel))
  28. viewController.present(alert, animated: true)
  29. }
  30. ///在根视图控制器上弹出普通消息提示框
  31. static func showAlert(message: String) {
  32. if let vc = UIApplication.shared.keyWindow?.rootViewController {
  33. showAlert(message: message, in: vc)
  34. }
  35. }
  36. ///在指定视图控制器上弹出确认框
  37. static func showConfirm(title:String? = nil, message: String, in viewController: UIViewController,
  38. confirm: ((UIAlertAction)->Void)?) {
  39. let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
  40. alert.addAction(UIAlertAction(title: "取消", style: .cancel))
  41. alert.addAction(UIAlertAction(title: "确定", style: .default, handler: confirm))
  42. viewController.present(alert, animated: true)
  43. }
  44. ///在根视图控制器上弹出确认框
  45. static func showConfirm(title:String? = nil, message: String, confirm: ((UIAlertAction)->Void)?) {
  46. if let vc = UIApplication.shared.keyWindow?.rootViewController {
  47. showConfirm(title:title,message: message, in: vc, confirm: confirm)
  48. }
  49. }
  50. /// 调用相机相册
  51. ///
  52. /// - Parameters:
  53. /// - camera: 相机
  54. /// - album: 相册
  55. static func showConfirmActionSheet(camera: ((UIAlertAction)->Void)?,album: ((UIAlertAction)->Void)?) {
  56. let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
  57. let cancelAction = UIAlertAction(title: "取消", style: .cancel)
  58. cancelAction.setValue(kThemeColor, forKey: "titleTextColor")
  59. alert.addAction(cancelAction)
  60. let cameraAction = UIAlertAction(title: "拍照", style: .default, handler: camera)
  61. cameraAction.setValue(kThemeColor, forKey: "titleTextColor")
  62. alert.addAction(cameraAction)
  63. let albumAction = UIAlertAction(title: "从手机相册选取", style: .default, handler: album)
  64. albumAction.setValue(kThemeColor, forKey: "titleTextColor")
  65. alert.addAction(albumAction)
  66. let viewController = getCurrentVC()
  67. viewController?.present(alert, animated: true)
  68. }
  69. }