gpt4 book ai didi

swift - 在 Swift 中获取 UIAlertAction 的处理程序

转载 作者:可可西里 更新时间:2023-11-01 00:53:18 27 4
gpt4 key购买 nike

如何在 Swift 中获取 UIAlertAction 的处理程序。它是在初始化时设置的,但是我还没有找到任何属性来控制操作的关闭。闭包的类型为 (UIAlertAction) -> Void 但是我想获取闭包的内容,以便我有一些像 () -> Void 这样的闭包。这可能吗?感谢您的回答

最佳答案

我已经为此创建了一个子类,如下所示:

/// An UIAlertAction which saves the handler. Can be used for unit testing the action callback.
final class UIExecutableAlertAction: UIAlertAction {

private var handler: ((UIAlertAction) -> Swift.Void)?

static func with(title: String?, style: UIAlertActionStyle, handler: ((UIAlertAction) -> Swift.Void)? = nil) -> UIExecutableAlertAction {
let action = UIExecutableAlertAction(title: title, style: style, handler: handler)
action.handler = handler
return action
}

func execute() {
handler?(self)
}

}

可以这样使用:

let myAction = UIExecutableAlertAction.with(title: "title", style: .destructive, handler: { [weak self] _ in
// Do something
})

关于swift - 在 Swift 中获取 UIAlertAction 的处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29084051/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com