Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. import UIKit
  2.  
  3.  
  4. public extension UIViewController{
  5.  
  6. class func instantiate<T: UIViewController>() -> T {
  7.  
  8. let storyboard = UIStoryboard(name: "Main", bundle: nil)
  9. let identifier = String(describing: self)
  10. return storyboard.instantiateViewController(withIdentifier: identifier) as! T
  11. }
  12.  
  13.  
  14.  
  15.  
  16. @discardableResult func showAlert(title: String?, message: String?, buttonTitles: [String]? = nil, highlightedButtonIndex: Int? = nil, completion: ((Int) -> Void)? = nil) -> UIAlertController {
  17. let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  18. var allButtons = buttonTitles ?? [String]()
  19. if allButtons.count == 0 {
  20. allButtons.append("OK")
  21. }
  22.  
  23. for index in 0..<allButtons.count {
  24. let buttonTitle = allButtons[index]
  25. let action = UIAlertAction(title: buttonTitle, style: .default, handler: { (_) in
  26. completion?(index)
  27. })
  28. alertController.addAction(action)
  29. // Check which button to highlight
  30. if let highlightedButtonIndex = highlightedButtonIndex, index == highlightedButtonIndex {
  31. if #available(iOS 9.0, *) {
  32. alertController.preferredAction = action
  33. }
  34. }
  35. }
  36. present(alertController, animated: true, completion: nil)
  37. return alertController
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement