Guest User

Untitled

a guest
Feb 22nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. class AlertView: NSObject {
  2.  
  3. class func showAlert(view: UIViewController , message: String){
  4.  
  5. let alert = UIAlertController(title: "Warning", message: message, preferredStyle: UIAlertControllerStyle.Alert)
  6. alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
  7. view.presentViewController(alert, animated: true, completion: nil)
  8. }
  9. }
  10.  
  11. class ViewController: UIViewController {
  12.  
  13. override func viewDidLoad() {
  14. super.viewDidLoad()
  15. AlertView.showAlert(self, message: "Test alert")
  16. // Do any additional setup after loading the view, typically from a nib.
  17. }
  18. }
  19.  
  20. class AlertView: NSObject {
  21.  
  22. class func showAlert(view: UIViewController , message: String) {
  23. let alert = UIAlertController(title: "Warning", message: message, preferredStyle: UIAlertControllerStyle.Alert)
  24. alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
  25.  
  26. dispatch_async(dispatch_get_main_queue(), {
  27. view.presentViewController(alert, animated: true, completion: nil)
  28. })
  29. }
  30. }
  31.  
  32. class ViewController: UIViewController {
  33.  
  34. override func viewDidLoad() {
  35. super.viewDidLoad()
  36. AlertView.showAlert(self, message: "Test alert")
  37. // Do any additional setup after loading the view, typically from a nib.
  38. }
  39. }
  40.  
  41. import UIKit
  42.  
  43. protocol AlertController { }
  44. extension AlertController where Self: UIViewController {
  45.  
  46. func showAlert(title title: String, message: String) {
  47.  
  48. let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
  49.  
  50. let okAction = UIAlertAction(title: "OK", style: .Cancel) { _ in }
  51. alertController.addAction(okAction)
  52.  
  53. view?.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
  54. }
  55.  
  56. func showAlertWithSettings(title title: String, message: String) {
  57.  
  58. let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
  59.  
  60. let okAction = UIAlertAction(title: "OK", style: .Cancel) { _ in }
  61. alertController.addAction(okAction)
  62.  
  63. let settingsAction = UIAlertAction(title: "Settings", style: .Default) { _ in
  64. guard let url = NSURL(string: UIApplicationOpenSettingsURLString) else { return }
  65. UIApplication.sharedApplication().openURL(url)
  66. }
  67. alertController.addAction(settingsAction)
  68.  
  69. view?.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
  70. }
  71. }
  72.  
  73. class ViewController: UIViewController, AlertController { }
  74.  
  75. showAlert(title: "Alert title", message: "Alert message")
  76.  
  77. class AlertView: NSObject {
  78. class func showAlert(view: UIViewController , title: String , message: String){
  79. let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
  80. alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
  81. view.present(alert, animated: true, completion: nil)
  82. }
  83. }
  84.  
  85. //How to use?
  86. //Alert.showAlert(view: self, title: "Alert!", message: "Reusable Alert👍🏿")
Add Comment
Please, Sign In to add comment