【发布时间】:2020-10-05 03:23:45
【问题描述】:
点击该按钮时,我试图从包含类属性的数组中打印属性,我已经阅读了一些答案,但没有一个有效。显然 println() 用于工作,但它似乎已被贬值。以下是有问题的代码:
这是按钮类
class Buttons{
var nature = UIButton(type: UIButton.ButtonType.custom)
var name: String
init ( name: String) {
self.name = name
}
}
这是主类中的数组
var buttonsArray=[
Buttons(name: "below")
,
Buttons(name: "front")
,
Buttons(name: "right")
]
映射:
for val in buttonsArray{
val.nature.frame = CGRect(x: 2, y: 10, width: 20, height:20)
val.nature.layer.cornerRadius = 0.5 * 0.12*bounds.size.width
val.nature.clipsToBounds = true
val.nature.backgroundColor = .blue
sceneView.addSubview(val.nature)
val.nature.adjustsImageWhenHighlighted = false
val.nature.addTarget(self, action: #selector(profileButtonClicked), for: UIControl.Event.touchDown)
val.nature.addTarget(self, action: #selector(profileButtonOut), for: UIControl.Event.touchUpInside)
val.nature.addTarget(self, action: #selector(profileButtonOut), for: UIControl.Event.touchDragExit)
}
分配函数
@objc func profileButtonClicked(button: self){
print(button.name)
}
我在功能想法中遇到错误,但重点是打印点击的按钮名称。 感谢您的宝贵时间
【问题讨论】: