X.BOZO X.BOZO - 1 year ago 660
Swift Question

how to clear text field?

i have Question about TextFiled in swift

how can i clear text in TextFiled when i click on it

my textfiled.text = "0"

i want when i click on textfiled remove number "0" automatica

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() {

fi.text = "0"


override func 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" + "---"



Answer Source

Use this method,

if you manually clear the textField use this below code,

fi.text = ""

or If you textfield click Textfield is empty, use this below delegate method,

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

hope its helpful