Search code examples
iosswiftuser-interfaceaddtarget

Trouble adding a new "Send Event" to a set of UITextFields


Errors I'm getting:-

I have 81 UITextFields that I want to add another Send Event too. Currently, they are all set to trigger one function for "Did End on Exit" but I want to add "Editing Changed" to all of them in order to call the same function. I know I could draw lines for all 81 TextFields, but I know there is the addTarget function, but I am not sure how it works. TIA.

//My attempt at addTarget
textField.addTarget(self, action: #selector(editedBox(sender:)), for: .editingChanged)

//Creates outlet for boxes
@IBOutlet var textFieldCollection: [UITextField]!

//Action if a box is edited
@IBAction func editedBox(_ sender: AnyObject)  {

}

Solution

  • here is the method ... textField is your text field .. if you have 81 fields ... you need to loop around and add target to all of them ....

    override func viewDidLoad() {
        super.viewDidLoad()
        for textField in textFieldCollection {
            textField.addTarget(self, action: #selector(editedBox(sender:)), for: .editingChanged)
        }
    
    
    
    
    }
    @objc func editedBox(sender: AnyObject)  {
    
    
    }