ExtensionUIViewController.swift 1.1 KB

1234567891011121314151617181920212223242526
  1. import Foundation
  2. import UIKit
  3. extension UIViewController{
  4. typealias action = (UIAlertAction)->Void
  5. func showAlert(_ title:String, _ message:String, _ ok:String? = nil, _ cancel:String? = nil, _ okAction:action? = nil, _ cancelAction:action? = nil, _ style:UIAlertController.Style = .alert){
  6. let alert = UIAlertController(title: title, message: message, preferredStyle: style)
  7. if let ok = ok{
  8. alert.addAction(UIAlertAction(title: ok, style: .default, handler: okAction))
  9. }
  10. if let cancel = cancel{
  11. alert.addAction(UIAlertAction(title: cancel, style: .default, handler: cancelAction))
  12. }
  13. // show action sheet
  14. if let popoverController = alert.popoverPresentationController {
  15. popoverController.sourceView = self.view
  16. popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
  17. popoverController.permittedArrowDirections = []
  18. }
  19. self.present(alert, animated: true, completion: nil)
  20. }
  21. }