VLCAlertViewController.swift 2.6 KB

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