Ios 如何将函数作为参数传递给警报操作?
这是我的全局函数。我想把动作作为另一个函数传递。所以请给我任何解决方法Ios 如何将函数作为参数传递给警报操作?,ios,swift,uialertview,Ios,Swift,Uialertview,这是我的全局函数。我想把动作作为另一个函数传递。所以请给我任何解决方法 showAlert(title: "Title", message: "Hello", viewController: self, action: self.deactivateViewOne()) func showAlert(title: String, message: String, viewController: UIViewController, action: -----){ let ale
showAlert(title: "Title", message: "Hello", viewController: self, action: self.deactivateViewOne())
func showAlert(title: String, message: String, viewController: UIViewController, action: -----){
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: UIAlertAction.Style.cancel, handler: nil))
alert.addAction(UIAlertAction(title: NSLocalizedString("Deactivate", comment: ""), style: UIAlertAction.Style.cancel, handler: { (alert) in
action
}))
viewController.present(alert, animated: true, completion: nil)
let subview = (alert.view.subviews.first?.subviews.first?.subviews.first!)! as UIView
subview.layer.cornerRadius = 10
subview.backgroundColor = Common.backgroundColor
subview.layer.borderWidth = 0.5
subview.layer.borderColor = Common.fontColor.cgColor
}
func deactivateViewOne() {
self.viewOne.layer.borderColor = Common.greyColor.cgColor
self.viewOneStartTime.textColor = Common.greyColor
self.viewOneStopTime.textColor = Common.greyColor
self.ViewOneHeatLabel.textColor = Common.greyColor
self.ViewOneUhrLabel.textColor = Common.greyColor
self.ViewOneDashLabel.textColor = Common.greyColor
self.viewOneStartTime.text = "--:--"
self.viewOneStopTime.text = "--:--"
}
只需使用闭包
func showAlert(title: String, message: String, viewController: UIViewController, action: @escaping () -> ()) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: UIAlertAction.Style.cancel, handler: nil))
alert.addAction(UIAlertAction(title: NSLocalizedString("Deactivate", comment: ""), style: UIAlertAction.Style.default, handler: { (alert) in
action()
}))
viewController.present(alert, animated: true, completion: nil)
let subview = (alert.view.subviews.first?.subviews.first?.subviews.first!)! as UIView
subview.layer.cornerRadius = 10
subview.backgroundColor = Common.backgroundColor
subview.layer.borderWidth = 0.5
subview.layer.borderColor = Common.fontColor.cgColor
}
并使用:
showAlert(title: "Title", message: "Hello", viewController: self, action: self.deactivateViewOne)
只需使用闭包
func showAlert(title: String, message: String, viewController: UIViewController, action: @escaping () -> ()) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: UIAlertAction.Style.cancel, handler: nil))
alert.addAction(UIAlertAction(title: NSLocalizedString("Deactivate", comment: ""), style: UIAlertAction.Style.default, handler: { (alert) in
action()
}))
viewController.present(alert, animated: true, completion: nil)
let subview = (alert.view.subviews.first?.subviews.first?.subviews.first!)! as UIView
subview.layer.cornerRadius = 10
subview.backgroundColor = Common.backgroundColor
subview.layer.borderWidth = 0.5
subview.layer.borderColor = Common.fontColor.cgColor
}
并使用:
showAlert(title: "Title", message: "Hello", viewController: self, action: self.deactivateViewOne)
应用程序因此错误而崩溃“UIAlertController只能有一个样式为UIAlertActionStyleCancel的操作”很抱歉!我已经更新了我的答案。(UIAlertAction.Style.default-更改为此)应用程序因此错误而崩溃“UIAlertController只能有一个样式为UIAlertActionStyleCancel的操作”很抱歉!我已经更新了我的答案。(UIAlertAction.Style.default-更改为此)