VLCAlertViewController.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. var action: AlertAction
  17. init(title: String, action: @escaping AlertAction) {
  18. self.title = title
  19. self.action = action
  20. }
  21. }
  22. @objcMembers class VLCAlertViewController: UIAlertController {
  23. class func alertViewManager(title: String, errorMessage: String? = nil, viewController: UIViewController,
  24. buttonsAction: [VLCAlertButton]) {
  25. let alert = UIAlertController(title: title, message: errorMessage, preferredStyle: .alert)
  26. for buttonAction in buttonsAction {
  27. let action = UIAlertAction(title: buttonAction.title, style: UIAlertActionStyle.default, handler: buttonAction.action)
  28. alert.addAction(action)
  29. }
  30. alert.show(viewController, sender: Any?.self)
  31. viewController.present(alert, animated: true, completion: nil)
  32. }
  33. class func alertManagerWithTextField(title: String, errorMessage: String? = nil, viewController: UIViewController,
  34. buttonsAction: [VLCAlertButton],
  35. textFieldText: String? = nil,
  36. textFieldPlaceholder: String? = nil) {
  37. let alert = UIAlertController(title: title, message: errorMessage, preferredStyle: .alert)
  38. alert.addTextField(configurationHandler: { textField in
  39. textField.placeholder = textFieldPlaceholder
  40. textField.text = textFieldText
  41. })
  42. for buttonAction in buttonsAction {
  43. let action = UIAlertAction(title: buttonAction.title, style: UIAlertActionStyle.default, handler: buttonAction.action)
  44. alert.addAction(action)
  45. }
  46. alert.show(viewController, sender: Any?.self)
  47. viewController.present(alert, animated: true, completion: nil)
  48. }
  49. }