我試圖通過UIBarButtonItem
作爲參數, 但我得到以下錯誤:如何傳遞參數給法「#selector」
Argument of '#selector' does not refer to an '@objc' method, property, or initializer
@objc func barClicked(sender: AnyObject) {
}
override func viewDidLoad() {
super.viewDidLoad()
let bar: UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 50))
bar.barStyle = UIBarStyle.default
bar.items = [
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil),
UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(barClicked(sender: bar.items[0])))]
bar.sizeToFit()
}
也試過#selector(barClicked(sender: bar.items[0] as AnyObject
謝謝
請看看「相關」部分,這已經被反覆詢問和回答。你*不能*在選擇器中傳遞參數。 –
僅供參考 - 該按鈕將是參數。你不需要自己指定它。 – rmaddy
@MartinR謝謝,是否有其他解決方案? –