Я пытаюсь добавить определенные c обработчики и alertStyles к моим оповещениям, но я использую глобальное оповещение, у которого нет обработчика - PullRequest
0 голосов
/ 20 февраля 2020

Есть ли отдельная глобальная функция для добавления другого стиля и другого обработчика для оповещений? Моя функция из AppDelegate выглядит следующим образом:

static func showAlertView(vc : UIViewController, titleString : String , messageString: String) ->()
 {
   let alertView = UIAlertController(title: titleString, message: messageString, preferredStyle: .alert)

   let alertAction = UIAlertAction(title: "ok", style: .cancel) { (alert) in
      vc.dismiss(animated: true, completion: nil)
   }
     alertView.addAction(alertAction)
     vc.present(alertView, animated: true, completion: nil)
 }

1 Ответ

0 голосов
/ 20 февраля 2020

Вам просто нужно добавить больше параметров для вашей функции. В приведенном ниже коде я добавил следующее: controllerStyle для UIAlertController, actionStyle для UIAlertAction и action для обработчика UIAlertAction.

static func showAlertView(vc : UIViewController, titleString : String , messageString: String, controllerStyle: UIAlertController.Style = .alert, actionStyle: UIAlertAction.Style = .cancel, action:  @escaping () -> () = {}) {
    let alertView = UIAlertController(title: titleString, message: messageString, preferredStyle: controllerStyle)
    let alertAction = UIAlertAction(title: "ok", style: .cancel) { (alert) in
        if action == {} {
            vc.dismiss(animated: true, completion: nil)
        } else {
            action()
        }
    }
    alertView.addAction(alertAction)
    vc.present(alertView, animated: true, completion: nil)
}
...