Skip to content

Instantly share code, notes, and snippets.

@farnscosnippet
Last active January 8, 2018 21:12
Show Gist options
  • Select an option

  • Save farnscosnippet/1029126444f3cd70cfc71ccc3741a4d4 to your computer and use it in GitHub Desktop.

Select an option

Save farnscosnippet/1029126444f3cd70cfc71ccc3741a4d4 to your computer and use it in GitHub Desktop.
SWIFT - Generic Alert
// Error Alert - Usage Example: showAlert(withTitle: "Oops", message: "Please fill in both gravity fields.", viewController: self)
func showAlert(withTitle title: String, message: String, viewController: UIViewController) {
if viewController.presentedViewController == nil { // Prevent multiple alerts at the same time
let localizedTitle = NSLocalizedString(title, comment: "")
let localizedMessage = NSLocalizedString(message, comment: "")
let alert = UIAlertController(title: localizedTitle, message: localizedMessage, preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(action)
viewController.present(alert, animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment