UIButton addTarget Selector not working

SquareBox.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

class GameViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let boxRow = SquareBox() 
        boxRow.createBoxes()
    }
}

      

Also I tried @IBAction instead of @objc, it doesn't work, but if I use the "click" function in ViewController.swift, I created this object, it works, but I need this function inside this class.

0


source to share


6 answers


Now that you've posted the relevant information in your question, the problem is perfectly clear. You have a memory management problem.

viewDidLoad

A local instance is created in your GameViewController SquareBox

. This local instance is out of scope at the end viewDidLoad

. Since there is no other link in this instance, it is freed at the end viewDidLoad

.

Since the instance has SquareBox

been freed, it will not act as the target of the button. And your method is click

never called.



The solution is to store the link in the view controller:

class GameViewController: UIViewController {
    let boxRow = SquareBox() 

    override func viewDidLoad() {
        super.viewDidLoad()
        boxRow.createBoxes()
    }
}

      

0


source


 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                       
}

      



+3


source


Replace this:

    btn.addTarget(self, action: #selector(self.click(sender:)), for: .touchUpInside)

      

enter image description here

I think something else is affecting your selection method when trying to find in your code, because your code also works in my project.

enter image description here

0


source


button.addTarget(self, action: #selector(self.buttonTapped), for: .touchUpInside)

func buttonTapped(sender : UIButton) {
       // code here
    }

      

0


source


button.addTarget(self, action:#selector(self.click), for: .touchUpInside)

func click(sender : UIButton) {
       // code here
    }

      

0


source


I think the problem is with how you customize the layout of your buttons. Try the following:

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")
}

      

0


source







All Articles