Swift 我试图将特定的处理程序和alertStyles添加到我的警报中,但我使用的是一个全局警报,它不';我没有训练员

Swift 我试图将特定的处理程序和alertStyles添加到我的警报中,但我使用的是一个全局警报,它不';我没有训练员,swift,uialertaction,Swift,Uialertaction,是否有任何单独的全局函数可以为警报添加不同的样式和不同的处理程序? AppDelegate中的我的函数如下所示: static func showAlertView(vc : UIViewController, titleString : String , messageString: String) ->() { let alertView = UIAlertController(title: titleString, message: messageString, prefer

是否有任何单独的全局函数可以为警报添加不同的样式和不同的处理程序?
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)
 }

您只需要向函数中添加更多参数。在下面的代码中,我添加了以下内容:
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)
}

你说的不同风格和不同的处理方式是什么意思?如果所说的不同处理程序是指另一个处理程序,则可以随时将另一个操作添加到
alertView
,如果所说的样式是指当前支持的样式。是的,最好的方法是在我需要添加特定处理程序时在我的代码中添加另一个警报。。因为全局函数不允许我添加处理程序。它与OP添加的不一样吗?简单地创建一个方法并没有给它增加任何区别,事实并非如此。OP希望在她的方法中有更多的配置,而这些配置在她共享的代码中是缺失的。我相信她知道如何添加
controllerStyle
actionStyle
,但她不知道方法可以作为变量传递给函数。这是我在回答中添加的内容。谢谢!我想知道的是,我是否可以在整个项目中只使用一个全局警报函数,即使我有时会为任何AletR使用处理程序或其他样式。但我认为在我有特定处理程序的地方,我必须添加另一个警报。这是真的。此解决方案仅适用于具有1个可自定义操作的警报控制器。如果要添加更多,则必须有特定的处理程序。