Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear the text field automatically

I have a question about UITextField() in Swift. How can I clear the text in the text field when I click on it?

My textField.text = "0". I want to automatically remove the number "0" when I click on the text field:

import UIKit
class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet var lbltext: UILabel!
    @IBOutlet var scrolview1: UIScrollView!
    @IBOutlet var fi: UITextField!
    @IBOutlet var scrolviewus: UIScrollView!
    @IBOutlet var counterLabel: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        fi.text = "0"
       }

       override func didReceiveMemoryWarning() {
         super.didReceiveMemoryWarning()
         // Dispose of any resources that can be recreated.
       }

       @IBAction func button(sender: AnyObject) {
        lbltext.numberOfLines = 0
        lbltext.text! = lbltext.text! + "\n" + fi.text! + "\n" + "---  "
    }
 }
like image 371
X.BOZO Avatar asked May 07 '16 04:05

X.BOZO


People also ask

How do you clear the text field in HTML?

To clear all the input in an HTML form, use the <input> tag with the type attribute as reset.

How do I clear the input field cache?

That solution helped me: if you are using google Chrome, try this: Put the mouse pointer on the entry that you want to delete, press Delete . If the entry is not removed, press Shift + Delete .


2 Answers

Use this method,

If you want to manually clear the text field use this code:

textField.text = ""

If you want the text field to empty when you begin editing, use the delegate method below:

func textFieldDidBeginEditing(textField: UITextField) {
    textField.text = ""
}

If you are using multiple text fields, use the method below:

func textFieldDidBeginEditing(textField: UITextField) { 
    if (textField == oneTextField) {
       textField.text = ""
    } 
    else if (textField == anotherTextField) {
       // Perform any other operation
    } 
}
like image 54
Iyyappan Ravi Avatar answered Sep 23 '22 03:09

Iyyappan Ravi


You can easily clear the TextField in swift

emailTextField.text?.removeAll()
passwordTextField.text?.removeAll()
confirmPasswordTextField.text?.removeAll()
like image 36
Apps Freax Avatar answered Sep 25 '22 03:09

Apps Freax