如何在UIAlertAction

时间:2019-12-12 18:39:38

标签: ios swift closures uialertcontroller uialertaction

我有几个按钮,单击这些按钮会显示相同的警报。我必须分别处理每个按钮的单击,因此我需要将不同的处理程序传递给每个警报。我该怎么办?

我一直在寻找解决方案,但是找不到我想要的东西,或者也许它在那儿,但我听不懂。

以下是我的代码段。在此函数内,我可以单击哪个按钮,但无法弄清楚如何调用不同的处理程序并将它们传递给alert.title。

我希望有人能指出我正确的方向。

@IBAction func buttonClicked(_ sender: UIButton) {

    let alert = UIAlertController(title: "Select Value", message: nil, preferredStyle: .actionSheet)

    for list in self.listValue {
        alert.addAction(UIAlertAction(title: list.value, style: .default, handler: { _ in

            // How do I call different handlers here?
            // I'll need to retrieve alert.title in these handlers

        }))
    }

    alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil))
    self.present(alert, animated: false, completion: nil)
}

1 个答案:

答案 0 :(得分:3)

尚不清楚您要问什么,但是如果您想弄清楚按下了哪个按钮,以便可以为每个按钮执行不同的方法,则可以执行以下操作:

/// Incrementally lighten a color in a more effective way than with lighten()
/// @param {Color} $color - color to tint
/// @param {Number} $percentage - Percentage of white in the returned color
/// @return {Color} - The lightened color
@function tint($color, $percentage) {
  @return mix(#fff, $color, $percentage);
}

/// Incrementally darken a color in a more effective way than with darken()
/// @param {Color} $color - Color to shade
/// @param {Number} $percentage - Percentage of black in the returned color
/// @return {Color} - The darkened color
@function shade($color, $percentage) {
  @return mix(#000, $color, $percentage);
}