Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将函数作为参数传递给警报操作?_Ios_Swift_Uialertview - Fatal编程技术网

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-更改为此)