'Clear all Text Fields button Swift
I'm making an app where users put numbers into 4 different textfields and it does something with them on the backend. Right now if they want to change those numbers they have to go back and delete them all and retype them or close the app and reopen it to have all the text fields blank again.
I want to add a button to the bottom of the app that says something like Clear Text and it will clear all 4 textfields.
I've tried looking it up but couldn't find anything like this, Only to make a clear text button appear when editing that text field. I'm looking to clear 4 text fields when a Button is clicked.
Solution 1:[1]
This should work:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBOutlet var field1: UITextField!
@IBOutlet var field2: UITextField!
@IBOutlet var field3: UITextField!
@IBOutlet var field4: UITextField!
@IBAction func buttonThatClears(_ sender: UIButton) {
field1.text = ""
field2.text = ""
field3.text = ""
field4.text = ""
field1.becomeFirstResponder()
}
}
Likely will look something like this:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nick Khotenko |

