gpt4 book ai didi

ios - addTarget 到 UIButton 以编程方式给我 'unrecognized selector sent to class'

转载 作者:行者123 更新时间:2023-11-28 06:40:31 25 4
gpt4 key购买 nike

我正在尝试学习如何创建按钮目标操作,但是,当我按下按钮时,出现了 LLDB 错误,并且我被告知这是“无法识别的选择器发送到类”。

我哪里错了?

StatusCell.swift:

let phoneIcon: UIButton = {
let iv = UIImageView()

iv.translatesAutoresizingMaskIntoConstraints = false
iv.image = UIImage(named: "Phone3")?.imageWithRenderingMode(.AlwaysTemplate)

let phoneBtn = UIButton(type: .Custom)

phoneBtn.addTarget(CallButton.self, action: #selector(CallButton.buttonPressed(_:)), forControlEvents: .TouchDown)
phoneBtn.addTarget(CallButton.self, action: #selector(CallButton.buttonReleased(_:)), forControlEvents: .TouchUpInside)

phoneBtn.translatesAutoresizingMaskIntoConstraints = false
phoneBtn.setImage(iv.image!, forState: .Normal)
phoneBtn.tintColor = UIColor(r: 224, g: 224, b: 224)

return phoneBtn

}()

这是我调用 buttonPressed 和 buttonReleased 的 CallButton 类。

class CallButton: UIControl {

func buttonPressed(sender: AnyObject?) {
print("Pressed")
}
func buttonReleased(sender: AnyObject?) {
print("Let go")
}

}

最佳答案

参数 target 的值必须是 CallButton 的实例,而不是类型本身。

关于ios - addTarget 到 UIButton 以编程方式给我 'unrecognized selector sent to class',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38091912/

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