【发布时间】:2021-03-31 20:38:57
【问题描述】:
我使用 for 循环创建了按钮,并且我想在按下按钮时打印按钮编号。我该怎么做?
for x in 0..<5 {
let button = UIButton(frame: CGRect(x: CGFloat(x) * view.frame.size.width + 10 , y: 40, width: view.frame.size.width - 20, height: 30))
buttonKontrol = x
print(buttonKontrol)
button.setTitle("Button", for: .normal)
button.backgroundColor = .white
button.setTitleColor(.black, for: .normal)
button.addTarget(self, action: #selector(btntapped), for: .touchUpInside)
scrollView.addSubview(button)
}
和 objc 函数:
@objc func btntapped(_ sender: UIButton) {
print("button tapped")
}
【问题讨论】:
标签: ios swift objective-c xcode for-loop