UIViewController+Alert.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //
  2. // UIViewController+Alert.swift
  3. // MTP2_iOS
  4. //
  5. // Created by zhongyuan on 2018/5/10.
  6. // Copyright © 2018年 zhongyuan.All rights reserved.
  7. //
  8. import UIKit
  9. extension UIViewController {
  10. // MARK: - 提示信息框
  11. /// 显示提示信息框的方法(单个确定按钮)
  12. ///
  13. /// - Parameters:
  14. /// - title: 标题
  15. /// - message: 提示内容
  16. /// - handler: 确定按钮点击后的回调块
  17. func showHintController(title: String, message: String, comfirmTitle: String = "ok", handler: ((UIAlertAction) -> Swift.Void)? = nil) {
  18. let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  19. alertController.addAction(UIAlertAction(title: comfirmTitle, style: .cancel, handler: handler))
  20. alertController.view.tintColor = UIColor.darkText
  21. self.present(alertController, animated: true, completion: nil)
  22. }
  23. /// 显示警告信息框的方法(自动隐藏)
  24. ///
  25. /// - Parameters:
  26. /// - title: 标题
  27. /// - message: 提示内容
  28. /// - completion: 自动隐藏后的回调块
  29. func showWarnController(title: String, message: String, completion: (() -> Swift.Void)? = nil) {
  30. let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
  31. self.present(alertController, animated: true, completion: { () -> Void in
  32. let delayTime = DispatchTime.now() + Double(Int64(1 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
  33. DispatchQueue.main.asyncAfter(deadline: delayTime, execute: { () -> Void in
  34. alertController.dismiss(animated: true, completion: completion)
  35. })
  36. })
  37. }
  38. /// 显示提示确定信息框
  39. ///
  40. /// - Parameters:
  41. /// - title: 标题
  42. /// - message: 提示内容
  43. /// - handler: 确定信息后的回调块
  44. func showYesOrNOController(title: String, message: String, handler: ((UIAlertAction) -> Swift.Void)? = nil) {
  45. let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  46. alertController.addAction(UIAlertAction(title: "yes", style: .destructive, handler: handler))
  47. alertController.addAction(UIAlertAction(title: "no", style: .cancel, handler: nil))
  48. alertController.view.tintColor = UIColor.darkText
  49. self.present(alertController, animated: true, completion: nil)
  50. }
  51. /// 显示提示确定信息框
  52. ///
  53. /// - Parameters:
  54. /// - title: 标题
  55. /// - message: 提示内容
  56. /// - handler: 确定信息后的回调块
  57. func showCommitAlertController(title: String, message: String, handler: ((UIAlertAction) -> Swift.Void)? = nil) {
  58. let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  59. alertController.addAction(UIAlertAction(title: "ok", style: .destructive, handler: handler))
  60. alertController.addAction(UIAlertAction(title: "cancle", style: .cancel, handler: nil))
  61. alertController.view.tintColor = UIColor.darkText
  62. self.present(alertController, animated: true, completion: nil)
  63. }
  64. /// 显示 ActionSheetController 弹出框信息
  65. ///
  66. /// - Parameters:
  67. /// - title: 标题
  68. /// - message: 内容信息
  69. /// - cancelTitle: 取消按钮标题
  70. /// - cancelBlock: 取消执行块
  71. /// - actionSheetTitles: actionSheetTitles 标题
  72. /// - handler: actionSheet 执行块
  73. func showActionSheetController(title : String?, message : String?, cancelTitle : String?, cancelBlock: (() -> Void)?, actionSheetTitles: [String]?, handler: ((_ index: NSInteger, _ action: UIAlertAction?) -> Void)?) {
  74. /// show AlertController
  75. let alertController = UIAlertController.init(title: title, message: message, preferredStyle: .actionSheet)
  76. /// cancel Action
  77. if let title = cancelTitle {
  78. let cancelAction = UIAlertAction.init(title: title, style: .cancel, handler: { (action) in
  79. if cancelBlock != nil {
  80. cancelBlock!()
  81. }
  82. })
  83. alertController.addAction(cancelAction)
  84. }
  85. alertController.view.tintColor = UIColor.darkText
  86. /// ActionSheet
  87. if let titles = actionSheetTitles {
  88. /// 构建sheets
  89. for sheetTitle in titles {
  90. let actionSheetAction = UIAlertAction.init(title: sheetTitle, style: .default, handler: { (alertAction) in
  91. let index = titles.firstIndex(of: sheetTitle)
  92. if handler != nil {
  93. handler!(index!, alertAction)
  94. }
  95. })
  96. alertController.addAction(actionSheetAction)
  97. }
  98. }
  99. /// modal present alertController
  100. self.present(alertController, animated: true, completion: nil)
  101. }
  102. /// 显示 AlertController 弹出框信息
  103. ///
  104. /// - Parameters:
  105. /// - title: 标题
  106. /// - message: 内容信息
  107. /// - cancelTitle: 取消按钮标题
  108. /// - sureTitle: 确认按钮标题
  109. /// - cancelBlock: 取消执行块
  110. /// - sureBlock: 确认执行块
  111. func showAlertController(title: String?, message: String?, cancelTitle: String?, sureTitle: String?, cancelBlock:(() -> Void)?, sureBlock:(() -> Void)?) {
  112. /// show AlertController
  113. let alertController = UIAlertController.init(title: title, message: message, preferredStyle: .alert)
  114. /// cancel Action
  115. if cancelTitle != nil {
  116. let cancelAction = UIAlertAction.init(title: cancelTitle, style: .cancel, handler: { (action) in
  117. if cancelBlock != nil {
  118. cancelBlock!()
  119. }
  120. })
  121. alertController.addAction(cancelAction)
  122. }
  123. alertController.view.tintColor = UIColor.darkText
  124. /// sure Action
  125. if sureTitle != nil {
  126. let sureAction = UIAlertAction.init(title: sureTitle, style: .destructive, handler: { (action) in
  127. if sureBlock != nil {
  128. sureBlock!()
  129. }
  130. })
  131. alertController.addAction(sureAction)
  132. }
  133. /// modal present alertController
  134. self.present(alertController, animated: true, completion: nil)
  135. }
  136. /// 展示给定数量信息的弹出选择框
  137. ///
  138. /// - Parameters:
  139. /// - title: 弹出框标题
  140. /// - message: 弹出框提示信息
  141. /// - takeInfo: 携带信息
  142. /// - type: 弹出类型
  143. /// - clickBlock: 代码块
  144. func showPopupsController(title: String?, message: String?, titleArray: [String], takeInfo: Any? = nil,type: UIAlertController.Style = .alert, _ showCancel: Bool = true, clickBlock: @escaping ((_ value: String,_ rowNum: Int, _ takeInfo: Any?) -> Void)) {
  145. /// show AlertController
  146. let alertController = UIAlertController(title: title, message: message, preferredStyle: type)
  147. alertController.view.tintColor = UIColor.darkText
  148. if showCancel == true {
  149. /// cancel Action
  150. let cancelAction = UIAlertAction(title: "cancle", style: .cancel, handler: nil)
  151. alertController.addAction(cancelAction)
  152. }
  153. /// Actions
  154. for (index, title) in titleArray.enumerated() {
  155. let action = UIAlertAction(title: title, style: .default, handler: { (_) in
  156. clickBlock(title,index, takeInfo)
  157. })
  158. alertController.addAction(action)
  159. }
  160. let height: NSLayoutConstraint = NSLayoutConstraint(item: alertController.view!, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.lessThanOrEqual, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: self.view.frame.height * 0.50)
  161. alertController.view.addConstraint(height)
  162. /// modal present alertController
  163. self.present(alertController, animated: true, completion: nil)
  164. }
  165. /// 根据错误码展示提示信息
  166. ///
  167. /// - Parameter error: ErrorInfo
  168. func showErrorMessgae(error: ErrorInfo?) {
  169. if let error = error {
  170. if let errorDesc = ErrorUtils.getDesc(errorCode: error.retCode) {
  171. self.showHintController(title: "提示", message: errorDesc)
  172. } else if let retMsg = error.retMsg {
  173. self.showHintController(title: "提示", message: retMsg)
  174. } else {
  175. self.showHintController(title: "提示", message: "未知错误")
  176. }
  177. } else {
  178. self.showHintController(title: "提示", message: "系统繁忙")
  179. }
  180. }
  181. /// 当键盘弹起时解决输入框被遮挡问题
  182. ///
  183. /// - Parameter views: 需要使用软键盘输入的文本控件列表
  184. func moveView(views: [UIView]) {
  185. NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillChangeFrameNotification, object: nil, queue: nil) { [weak self] (notification) in
  186. guard let weakSelf = self, let responseView = views.first(where: { $0.isFirstResponder }) else { return }
  187. // 获取键盘的坐标
  188. let endFrame = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
  189. let keyboardY: CGFloat = kScreenHeight - endFrame.origin.y
  190. // 获取目标的Y值
  191. let targetY: CGFloat = responseView.convert(CGPoint(x: 0, y: responseView.height), to: weakSelf.view).y
  192. // 为防止遮挡需要偏移的距离
  193. let offset = targetY + keyboardY - weakSelf.view.height + 24 // 24是textfield和键盘上方的间距,可以自由设定
  194. UIView.animate(withDuration: 0.25) {
  195. if offset > 0 {
  196. weakSelf.view.bounds = CGRect(x: 0, y: offset, width: weakSelf.view.width, height: weakSelf.view.height)
  197. } else {
  198. weakSelf.view.bounds = CGRect(x: 0, y: 0, width: weakSelf.view.width, height: weakSelf.view.height)
  199. }
  200. }
  201. }
  202. }
  203. /// 找到以本viewController为基础中最顶层的viewController,若没有模态视图,则最顶层的viewController就是它本身
  204. var presentedViewControllerVisibleInWindow: UIViewController {
  205. var vc = self
  206. while (vc.presentedViewController != nil) {
  207. vc = vc.presentedViewController!
  208. }
  209. return vc
  210. }
  211. }
  212. extension UIViewController {
  213. /// viewController
  214. /// - Parameters:
  215. /// - name: name
  216. /// - identifier: identifier
  217. /// - Returns: UIViewController
  218. func viewController (name: String, identifier: String) -> UIViewController {
  219. return UIStoryboard(name: name, bundle: nil).instantiateViewController(withIdentifier: identifier)
  220. }
  221. }
  222. extension UIViewController {
  223. /// push
  224. /// - Parameters:
  225. /// - sbname: storyboardName
  226. /// - sbId: storyboardId
  227. /// - takeInfo: takeInfo
  228. /// - checkLogin: checkLogin
  229. func push(storyboardName sbname:String,
  230. storyboardId sbId:String,
  231. takeInfo: Any? = nil,
  232. checkLogin: Bool = true) {
  233. if !checkLogin || MTP2BusinessCore.shared.getLoginStatus() { /// 检查登录状态
  234. let board: UIStoryboard = UIStoryboard.init(name: sbname, bundle: nil)
  235. if let baseViewController = board.instantiateViewController(withIdentifier: sbId) as? BaseViewController,
  236. let model = takeInfo {
  237. baseViewController.takeInfo = model
  238. self.navigationController?.pushViewController(baseViewController, animated: true)
  239. } else {
  240. let viewController = board.instantiateViewController(withIdentifier: sbId)
  241. self.navigationController?.pushViewController(viewController, animated: true)
  242. }
  243. } else { /// 未登录
  244. let login = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Login")
  245. self.navigationController?.pushViewController(login, animated: true)
  246. }
  247. }
  248. /// present
  249. /// - Parameters:
  250. /// - sbname: sbname
  251. /// - sbId: sbId
  252. /// - isNav: isNav
  253. /// - style: modalTransitionStyle
  254. func present(storyboardName sbname:String,
  255. storyboardId sbId:String,
  256. needNavigationController isNav: Bool = true,
  257. modalTransitionStyle style: UIModalTransitionStyle = .crossDissolve) {
  258. let board: UIStoryboard = UIStoryboard.init(name: sbname, bundle: nil)
  259. let viewController = board.instantiateViewController(withIdentifier: sbId)
  260. if isNav == true {
  261. let navigationController = BaseNavigationController(rootViewController: viewController)
  262. navigationController.modalPresentationStyle = .overFullScreen
  263. navigationController.modalTransitionStyle = style
  264. self.present(navigationController, animated: true, completion: nil)
  265. } else {
  266. viewController.modalPresentationStyle = .overFullScreen
  267. viewController.modalTransitionStyle = style
  268. self.present(viewController, animated: true, completion: nil)
  269. }
  270. }
  271. }