VLCAlertViewController.swift 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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: AlertAction? = nil) {
  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. VLCAlertViewController.alertViewManager(title: title, errorMessage: errorMessage, viewController: viewController, buttonsAction: nil)
  25. }
  26. class func alertViewManager(title: String, errorMessage: String? = nil, viewController: UIViewController, buttonsAction: [VLCAlertButton]?) {
  27. let alert = UIAlertController(title: title, message: errorMessage, preferredStyle: .alert)
  28. if let buttonsAction = buttonsAction {
  29. for buttonAction in buttonsAction {
  30. let action = UIAlertAction(title: buttonAction.title, style: UIAlertActionStyle.default, handler: buttonAction.action)
  31. alert.addAction(action)
  32. }
  33. } else {
  34. let action = UIAlertAction(title: NSLocalizedString("BUTTON_OK", comment:""), style: UIAlertActionStyle.default, handler: nil)
  35. alert.addAction(action)
  36. }
  37. alert.show(viewController, sender: Any?.self)
  38. viewController.present(alert, animated: true, completion: nil)
  39. }
  40. class func alertManagerWithTextField(title: String, description: String? = nil,
  41. viewController: UIViewController,
  42. buttonsAction: [VLCAlertButton],
  43. textFieldText: String? = nil,
  44. textFieldPlaceholder: String? = nil) {
  45. let alert = UIAlertController(title: title, message: description, preferredStyle: .alert)
  46. alert.addTextField(configurationHandler: { textField in
  47. textField.placeholder = textFieldPlaceholder
  48. textField.text = textFieldText
  49. })
  50. for buttonAction in buttonsAction {
  51. let action = UIAlertAction(title: buttonAction.title, style: UIAlertActionStyle.default, handler: buttonAction.action)
  52. alert.addAction(action)
  53. }
  54. alert.show(viewController, sender: Any?.self)
  55. viewController.present(alert, animated: true, completion: nil)
  56. }
  57. }