skip to Main Content

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.

2

Answers


  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:

    enter image description here

    Login or Signup to reply.
  2. Select the Attributes inspector for the textField.

    In the first section, you have Clear button

    Below, select Clear when editing begins.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search