VLCAlertViewController.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*****************************************************************************
  2. * VLCAlertViewController.swift
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2018 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Quentin Richard <Quentinr75@gmail.com>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. import UIKit
  13. typealias AlertAction = (UIAlertAction) -> Void
  14. @objcMembers class VLCAlertButton: NSObject {
  15. let title: String
  16. let style: UIAlertActionStyle
  17. let action: AlertAction?
  18. init(title: String, style: UIAlertActionStyle = .default, action: AlertAction? = nil) {
  19. self.title = title
  20. self.style = style
  21. self.action = action
  22. }
  23. // For Objc
  24. convenience init(title: String, action: AlertAction? = nil) {
  25. self.init(title: title, action: action)
  26. }
  27. }
  28. @objcMembers class VLCAlertViewController: UIAlertController {
  29. class func alertViewManager(title: String, errorMessage: String? = nil, viewController: UIViewController) {
  30. VLCAlertViewController.alertViewManager(title: title, errorMessage: errorMessage, viewController: viewController, buttonsAction: nil)
  31. }
  32. class func alertViewManager(title: String, errorMessage: String? = nil, viewController: UIViewController, buttonsAction: [VLCAlertButton]?) {
  33. let alert = UIAlertController(title: title, message: errorMessage, preferredStyle: .alert)
  34. if let buttonsAction = buttonsAction {
  35. for buttonAction in buttonsAction {
  36. let action = UIAlertAction(title: buttonAction.title, style: buttonAction.style, handler: buttonAction.action)
  37. alert.addAction(action)
  38. }
  39. } else {
  40. let action = UIAlertAction(title: NSLocalizedString("BUTTON_OK", comment:""), style: UIAlertActionStyle.default, handler: nil)
  41. alert.addAction(action)
  42. }
  43. alert.show(viewController, sender: Any?.self)
  44. viewController.present(alert, animated: true, completion: nil)
  45. }
  46. class func alertManagerWithTextField(title: String, description: String? = nil,
  47. viewController: UIViewController,
  48. buttonsAction: [VLCAlertButton],
  49. textFieldText: String? = nil,
  50. textFieldPlaceholder: String? = nil) {
  51. let alert = UIAlertController(title: title, message: description, preferredStyle: .alert)
  52. alert.addTextField(configurationHandler: { textField in
  53. textField.placeholder = textFieldPlaceholder
  54. textField.text = textFieldText
  55. })
  56. for buttonAction in buttonsAction {
  57. let action = UIAlertAction(title: buttonAction.title, style: buttonAction.style, handler: buttonAction.action)
  58. alert.addAction(action)
  59. }
  60. alert.show(viewController, sender: Any?.self)
  61. viewController.present(alert, animated: true, completion: nil)
  62. }
  63. }