Created
January 18, 2021 14:46
-
-
Save mohsinbmwm3/4bd06dedda42d9cd3a50b2cd8d96c0de to your computer and use it in GitHub Desktop.
Some useful UIAlertController extension in swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension UIAlertController { | |
static func showSimpleAlert(title: String, message: String, context: UIViewController) { | |
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) | |
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil)) | |
context.present(alert, animated: true, completion: nil) | |
} | |
static func showSimpleAlert(title: String, message: String, okButtonTitle: String, okButtonAction: (@escaping () -> ()), context: UIViewController) { | |
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) | |
alert.addAction(UIAlertAction(title: okButtonTitle, style: .default, handler: { action in | |
okButtonAction() | |
})) | |
context.present(alert, animated: true, completion: nil) | |
} | |
static func showAlertWith2Button(title: String, message: String, firstButtonTitle: String, firstButtonAction: (@escaping () -> ()), secondButtonTitle: String, seconButtonAction: (@escaping () -> ()), context: UIViewController) { | |
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) | |
alert.addAction(UIAlertAction(title: firstButtonTitle, style: .default, handler: { action in | |
firstButtonAction() | |
})) | |
alert.addAction(UIAlertAction(title: secondButtonTitle, style: .destructive, handler: { action in | |
seconButtonAction() | |
})) | |
context.present(alert, animated: true, completion: nil) | |
} | |
static func showLogoutAlert(context: UIViewController) { | |
let alert = UIAlertController(title: "Logout", message: "You are logged out of the app successfully.", preferredStyle: .alert) | |
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil)) | |
context.present(alert, animated: true, completion: nil) | |
} | |
static func showInvlaidCredentialAlert(context: UIViewController) { | |
let alert = UIAlertController(title: "My App", message: "Invalid credentials.", preferredStyle: .alert) | |
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil)) | |
context.present(alert, animated: true, completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment