import Foundation import UIKit extension UIViewController{ typealias action = (UIAlertAction)->Void func showAlert(_ title:String, _ message:String, _ ok:String? = nil, _ cancel:String? = nil, _ okAction:action? = nil, _ cancelAction:action? = nil, _ style:UIAlertController.Style = .alert){ let alert = UIAlertController(title: title, message: message, preferredStyle: style) if let ok = ok{ alert.addAction(UIAlertAction(title: ok, style: .default, handler: okAction)) } if let cancel = cancel{ alert.addAction(UIAlertAction(title: cancel, style: .default, handler: cancelAction)) } // show action sheet if let popoverController = alert.popoverPresentationController { popoverController.sourceView = self.view popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0) popoverController.permittedArrowDirections = [] } self.present(alert, animated: true, completion: nil) } }