21

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" + "---  "
    }
 }
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
X.BOZO
  • 301
  • 1
  • 3
  • 12
  • 1
    Possibly related: http://stackoverflow.com/questions/28530167/objective-c-auto-erase-nstextfield-string – l'L'l May 07 '16 at 04:40
  • 1
    So what, it's the same concept. – l'L'l May 07 '16 at 04:41
  • i am first time using swift and i don't know how to objective-c – X.BOZO May 07 '16 at 04:43
  • 1
    Well, if you read the other question there is no answer, (obj-c and swift apis are similar in some ways) so it might be just that. If there is a way then hopefully someone will explain... good luck :) – l'L'l May 07 '16 at 04:45

8 Answers8

39

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
    } 
}
brimstone
  • 3,370
  • 3
  • 28
  • 49
Iyyappan Ravi
  • 3,205
  • 2
  • 16
  • 30
  • What if I have two textfiled, 1 , 2 , and when I click on the first 1textfiled will remove "0", and then when i click on second textfiled2 will remove "0", I want to return the value of the first textfiled when i click on textfiled2 or anywhere – X.BOZO May 07 '16 at 05:17
  • 1
    you want differentiate the textfield, check the condition below format, ` func textFieldDidBeginEditing(textField: UITextField) { if textField == fi { } else if textField == anotherTextFieldName { } }' check the condition – Iyyappan Ravi May 07 '16 at 05:28
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/111271/discussion-between-iyyappan-ravi-and-x-bozo). – Iyyappan Ravi May 07 '16 at 05:51
  • 1
    its check your textfield is equal to yourTextFieldName then call the particular textfield only this code is comment for you – Iyyappan Ravi May 07 '16 at 05:54
  • if textField == TextFailde.text { here what i write ?} – X.BOZO May 07 '16 at 06:00
  • 1
    write what you want `fi.text = ""` or you give any function in particular textfield, mainly particular textfield only call the action – Iyyappan Ravi May 07 '16 at 06:04
  • its small issue set the break point and check, ok Thank you bro – Iyyappan Ravi May 07 '16 at 07:18
  • i use this function (func textFieldDidEndEditing(textField: UITextField)) – X.BOZO May 07 '16 at 07:19
10

You can easily clear the TextField in swift

emailTextField.text?.removeAll()
passwordTextField.text?.removeAll()
confirmPasswordTextField.text?.removeAll()
Apps Freax
  • 113
  • 1
  • 6
6

Simply use textFieldDidBeginEditing method to handle focus change, and clear text.

func textFieldDidBeginEditing(textField: UITextField) {
    textField.text = ""
}
brimstone
  • 3,370
  • 3
  • 28
  • 49
4
func textFieldDidBeginEditing(textField: UITextField)
{
    if fi.text == "0"
    {
        fi.text = ""
    }
}
Jayesh Miruliya
  • 3,279
  • 2
  • 25
  • 22
2

There are a couple of ways you can solve this problem, depending on what you're looking for. The first is to use a placeholder instead of setting the text to 0. A placeholder will disappear whenever the user starts typing and reappear if they type nothing in:

fi.placeholder = "0"

if you want the 0 to be the value and not just a placeholder, there are lots of built in methods to track editing/entering/returning/etc of a textfield. This does require implementation of the UITextFieldDelegate however, which it appears you have already done.

var isFirstTime = true

override viewDidLoad() {
     super.viewDidLoad()
     fi.delegate = self
}

func textViewDidBeginEditing(textView: UITextView) {
    isFirstTime = false
    if fi.text == "0" {
        fi.text = ""
    }
}

func textViewDidEndEditing(textView: UITextView) {
    if fi.text.isEmpty {
        fi.text = "0"
        isFirstTime = true
    }
}

The is first time var is only present so that the user can type 0 if they choose. If the user should not be able to type 0, then it may be removed.

treyhakanson
  • 4,611
  • 2
  • 16
  • 33
  • thank you so much , your code doesn't work with me i think we should use func textFieldDidBeginEditing – X.BOZO May 07 '16 at 05:09
  • Whoops, misread the question that'll be correct. Essentially the same thing. Be careful with the other answers though as they are largely incomplete; if you always set the text to "" on begin editing than anytime the user enters the field whatever they have typed will be deleted. – treyhakanson May 07 '16 at 05:20
2
func textFieldDidBeginEditing(textField: UITextField) {
        fi.text = ""
    }

func textFieldDidEndEdition(textField: UITextField) {

     if fi.text isEqualToString:@"" {
          fi.text = @""
     }

}
Gaurav Patel
  • 532
  • 1
  • 5
  • 19
1

simply use clearsOnBeginEditing property of textfield.

textfield.clearsOnBeginEditing = true
0

Use a small button on the right corner of textfield. Create action of that button.

@IBAction func removeSearchTFTapped(_ sender: Any) {
    searchTF.text = ""
}
UsamaDev
  • 21
  • 1
  • 3