Home > Blockchain >  How do I create IBAction for a textfield made programmatically?
How do I create IBAction for a textfield made programmatically?

Time:02-01

I've made a UITextField in my view controller.

let label = UITextField(frame: CGRect(x: 0, y: 0, width: 300, height: 21))

I was wondering how I would make an IBAction for this.

I'm making a variable amount of text fields which when edited, I want to take their values and make grade for a class. Each textfields value is an individual assignment and are editable by the user, so when one is edited it recalculates the class grade.

CodePudding user response:

You don't need @IBAction, you just create a function that handles the event, and add it as addTarget:

label.addTarget(self, action: #selector(myTargetFunction), for: <event>)

where myTargetFunction is your function, e.g.:

@objc func myTargetFunction(_ sender: UITextField) { ...

and <event> is one of the UIControl events available for UITextField.

  • Related