VLCAlertViewController.swift 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. let alert = UIAlertController(title: title, message: errorMessage, preferredStyle: .alert)
  26. alert.show(viewController, sender: Any?.self)
  27. for buttonAction in buttonsAction {
  28. let action = UIAlertAction(title: buttonAction.buttonTitle, style: UIAlertActionStyle.default, handler: buttonAction.buttonAction)
  29. alert.addAction(action)
  30. }
  31. viewController.present(alert, animated: true, completion: nil)
  32. }
  33. @objc class func alertManagerWithTextField(title: String, errorMessage: String? = nil, viewController: UIViewController,
  34. buttonsAction: [ButtonAction], textFieldText: String? = nil,
  35. textFieldPlaceholder: String? = nil) {
  36. let alert = UIAlertController(title: title, message: errorMessage, preferredStyle: .alert)
  37. alert.show(viewController, sender: Any?.self)
  38. alert.addTextField(configurationHandler: { textField in
  39. textField.placeholder = textFieldPlaceholder
  40. textField.text = textFieldText
  41. textField.isSecureTextEntry = false
  42. textField.textAlignment = .left
  43. })
  44. for buttonAction in buttonsAction {
  45. let action = UIAlertAction(title: buttonAction.buttonTitle, style: UIAlertActionStyle.default, handler: buttonAction.buttonAction)
  46. alert.addAction(action)
  47. }
  48. viewController.present(alert, animated: true, completion: nil)
  49. }
  50. }