Create a global alert controller that is available in all view controllers using the extension.
Create a UIViewController extension with your function to display a warning with the required parameter arguments.
extension UIViewController { func displayalert(title:String, message:String) { let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert) alert.addAction((UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in alert.dismiss(animated: true, completion: nil) }))) self.present(alert, animated: true, completion: nil) } }
Now call this function from your view controller:
class TestViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.displayalert(title: <String>, message: <String>) } }
source share