Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- public extension UIViewController{
- class func instantiate<T: UIViewController>() -> T {
- let storyboard = UIStoryboard(name: "Main", bundle: nil)
- let identifier = String(describing: self)
- return storyboard.instantiateViewController(withIdentifier: identifier) as! T
- }
- @discardableResult func showAlert(title: String?, message: String?, buttonTitles: [String]? = nil, highlightedButtonIndex: Int? = nil, completion: ((Int) -> Void)? = nil) -> UIAlertController {
- let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
- var allButtons = buttonTitles ?? [String]()
- if allButtons.count == 0 {
- allButtons.append("OK")
- }
- for index in 0..<allButtons.count {
- let buttonTitle = allButtons[index]
- let action = UIAlertAction(title: buttonTitle, style: .default, handler: { (_) in
- completion?(index)
- })
- alertController.addAction(action)
- // Check which button to highlight
- if let highlightedButtonIndex = highlightedButtonIndex, index == highlightedButtonIndex {
- if #available(iOS 9.0, *) {
- alertController.preferredAction = action
- }
- }
- }
- present(alertController, animated: true, completion: nil)
- return alertController
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement