gpt4 book ai didi

ios - 是否可以将闭包传递给选择器?

转载 作者:行者123 更新时间:2023-11-28 13:49:06 24 4
gpt4 key购买 nike

<分区>

是否有可能在 swift 中将闭包传递给选择器?

例如,是否可以重写这段代码:

let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(doneAction(_:)))

@objc func doneAction(_ sender: UIBarButtonItem){
self.myTextField?.resignFirstResponder()
}

成为:

let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: { _ sender in 
self.myTextField?.resignFirstResponder()
})

我试过上面的代码并得到:

Cannot convert value of type '() -> ()' to expected argument type 'Selector'

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