[英]UIButton addTarget Selector is not working
SquareBox.swift方框.swift
class SquareBox {
func createBoxes() {
for _ in 0..<xy {
let button = UIButton()
button.backgroundColor = .white
button.setTitleColor(UIColor.black, for: .normal)
button.layer.borderWidth = 0.5
button.layer.borderColor = UIColor.black.cgColor
stack.addArrangedSubview(button)
button.addTarget(self, action: #selector(click(sender:)) , for: .touchUpInside)
}
}
@objc func click(sender : UIButton) {
print("Click")
}
}
ViewController.swift视图控制器.swift
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let boxRow = SquareBox()
boxRow.createBoxes()
}
}
Also I've tried @IBAction instead of @objc, it doesn't work, but if I use "click" function in ViewController.swift that I created this object, it's working but I need this function inside of this class.我也试过 @IBAction 而不是 @objc,它不起作用,但是如果我在我创建这个对象的 ViewController.swift 中使用“点击”函数,它可以工作,但我需要在这个类中使用这个函数。
Now that you have posted relevant information in your question, the problem is quite clear.既然你已经在你的问题中发布了相关信息,那么问题就很清楚了。 You have a memory management issue.
您有内存管理问题。
In your GameViewController's viewDidLoad
you create a local instance of SquareBox
.在您的 GameViewController 的
viewDidLoad
中,您创建了一个SquareBox
的本地实例。 This local instance goes out of scope at the end of viewDidLoad
.这个本地实例在
viewDidLoad
结束时超出范围。 Since there is no other reference to this instance, it gets deallocated at the end of viewDidLoad
.由于没有其他对此实例的引用,它在
viewDidLoad
结束时被释放。
Since the instance of SquareBox
has been deallocated, it is not around to act as the button's target.由于
SquareBox
的实例已被释放,它不再充当按钮的目标。 And your click
method is never called.并且您的
click
方法永远不会被调用。
The solution is to keep a reference in your view controller:解决方案是在视图控制器中保留一个引用:
class GameViewController: UIViewController {
let boxRow = SquareBox()
override func viewDidLoad() {
super.viewDidLoad()
boxRow.createBoxes()
}
}
var btnfirst:UIButton!
override func viewDidLoad()
{
super.viewDidLoad()
btnfirst = UIButton(type: .system)
btnfirst.setTitle("Press", for: .normal)
btnfirst.setTitleColor(.red, for: .normal)
btnfirst.frame = CGRect(x: 100, y: 200, width: 100, height: 30)
btnfirst.addTarget(self, action: #selector(benpress( sender:)),for: .touchUpInside)
self.view.addSubview(btnfirst)
}
func benpress( sender :UIButton)
{
//Your Code Here
}
For those who did not find a solution, here is mine.对于那些没有找到解决方案的人,这是我的。
If you constructed your UIButton
as如果您将
UIButton
构建为
let button: UIButton = {
return UIButton()
}()
Just convert those into只需将它们转换成
lazy var button: UIButton = {
return UIButton()
}()
I think this is because of somewhat deallocation as mentioned above.我认为这是因为如上所述有些重新分配。
button.addTarget(self, action:#selector(self.click), for: .touchUpInside)
func click(sender : UIButton) {
// code here
}
I guess the issue is how you are setting up layout of your buttons.我想问题是您如何设置按钮的布局。 Try this:
试试这个:
func createBoxes() {
stack.backgroundColor = UIColor.red
for _ in 0..<xy {
// Create the button
let button = UIButton()
button.backgroundColor = UIColor.red
// Add constraints
button.translatesAutoresizingMaskIntoConstraints = false
button.heightAnchor.constraint(equalToConstant: 44.0).isActive = true
button.widthAnchor.constraint(equalToConstant: 44.0).isActive = true
// Setup the button action
button.addTarget(self, action: #selector(SquareBox.click(sender:)), for: .touchUpInside)
// Add the button to the stack
stack.addArrangedSubview(button)
}
}
@objc func click(sender : UIButton) {
print("Click")
}
button.addTarget(self, action: #selector(self.buttonTapped), for: .touchUpInside)
func buttonTapped(sender : UIButton) {
// code here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.