UIButton addTarget doesn't get called

270 Views Asked by At

I'm trying to create a UIAlertController that has a UITextField inside.
The text field should have a button in its right side (using the rightView attribute). The button should view an image instead of a regular text button.

When I'm clicking the button of the text field, the function btnClicked should be called, but it never actually gets called.

let alert = UIAlertController(title: "Testing", message: "", preferredStyle: .alert)
            
alert.addTextField { (field) in
    let chk = UIButton(type: UIButtonType.custom)
    chk.frame = CGRect(x: 2, y: 2, width: 18, height: 18)
    chk.tag = i
    chk.isUserInteractionEnabled = true
    chk.addTarget(self, action: #selector(self.btnClicked(sender:)), for: .touchUpInside)
    // I also tried the following:
    // chk.addTarget(chk, action: #selector(self.btnClicked(sender:)), for: .touchUpInside)

                    
    chk.setImage(UIImage(named: "rectangle"), for: .normal)
    chk.setImage(UIImage(named: "rectangle"), for: .selected)
    chk.setImage(UIImage(named: "rectangle"), for: .highlighted)
    chk.imageView!.contentMode = UIViewContentMode.scaleAspectFit
    chk.adjustsImageWhenHighlighted = true
                    
    field.rightView = chk
    field.rightViewMode = .always
}
            
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
            
self.present(alert, animated: true, completion: nil)

This is the btnClicked function:

@objc func btnClicked(sender: UIButton) {
    print("test")
}

For now, I'm just trying to print test, just to see if it works, but nothing is getting printed to the console.
Am I doing something wrong? So far I couldn't manage to call the btnClicked function after clicking on the chk button.

2

There are 2 best solutions below

0
On BEST ANSWER

I have run your code, there is some compiler error, I have fixed them. It works perfectly. It prints "test" every time in the console whenever I tapped on the button.

 func showAlert(){
   let alert = UIAlertController(title: "Testing", message: "", preferredStyle: .alert)

    alert.addTextField { (field) in
        let chk = UIButton(type: UIButton.ButtonType.custom)
        chk.frame = CGRect(x: 2, y: 2, width: 18, height: 18)
        chk.tag = 1
        chk.isUserInteractionEnabled = true
        chk.addTarget(self, action: #selector(self.btnClicked(sender:)), for: .touchUpInside)
        // I also tried the following:
        // chk.addTarget(chk, action: #selector(self.btnClicked(sender:)), for: .touchUpInside)


        chk.setImage(UIImage(named: "rectangle"), for: .normal)
        chk.setImage(UIImage(named: "rectangle"), for: .selected)
        chk.setImage(UIImage(named: "rectangle"), for: .highlighted)
        chk.imageView!.contentMode = UIView.ContentMode.scaleAspectFit
        chk.adjustsImageWhenHighlighted = true

        field.rightView = chk
        field.rightViewMode = .always
    }

    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

    self.present(alert, animated: true, completion: nil)
}

@objc func btnClicked(sender: UIButton) {
    print("test")
}
6
On

Change target chk to self (or where your btnClicked function is in)

chk.addTarget(self, action: #selector(self.btnClicked(sender:)), for: .touchUpInside)