ios 如何自动清除文本字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37084537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:00:44  来源:igfitidea点击:

How to clear the text field automatically

iosswiftiphoneswift2uitextfield

提问by X.BOZO

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

我有一个关于UITextField()Swift的问题。单击文本字段时,如何清除文本字段中的文本?

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

我的textField.text = "0"。我想在单击文本字段时自动删除数字“0”:

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" + "---  "
    }
 }

回答by Iyyappan Ravi

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
    } 
}

回答by Apps Freax

You can easily clear the TextField in swift

您可以轻松地快速清除 TextField

emailTextField.text?.removeAll()
passwordTextField.text?.removeAll()
confirmPasswordTextField.text?.removeAll()

回答by brimstone

Simply use textFieldDidBeginEditingmethod to handle focus change, and clear text.

只需使用textFieldDidBeginEditing方法来处理焦点变化,并清除文本。

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

回答by Jayesh Miruliya

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

回答by Gaurav Patel

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

func textFieldDidEndEdition(textField: UITextField) {

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

}

回答by treyhakanson

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:

有几种方法可以解决此问题,具体取决于您要查找的内容。第一种是使用占位符而不是将文本设置为 0。当用户开始输入时,占位符将消失,如果他们没有输入任何内容,则占位符会重新出现:

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.

如果您希望 0 成为值而不仅仅是占位符,则有许多内置方法可以跟踪文本字段的编辑/输入/返回/等。但是,这确实需要实现 UITextFieldDelegate,这似乎您已经完成了。

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.

第一次 var 仅存在,以便用户可以选择键入 0。如果用户不能键入 0,则它可能会被删除。