如何在swift中检查文本字段是否为空

时间:2014-06-08 03:11:25

标签: swift textbox

我正在处理以下代码,检查textField1textField2文本字段是否有任何输入。

当我按下按钮时,IF语句没有做任何事情。

 @IBOutlet var textField1 : UITextField = UITextField()
 @IBOutlet var textField2 : UITextField = UITextField()
 @IBAction func Button(sender : AnyObject) 
  {

    if textField1 == "" || textField2 == "" 
      {

  //then do something

      }  
  }

16 个答案:

答案 0 :(得分:157)

简单地将textfield object 与空字符串""进行比较并不是正确的方法。您必须比较文本字段的text属性,因为它是兼容类型并保存您要查找的信息。

@IBAction func Button(sender: AnyObject) {
    if textField1.text == "" || textField2.text == "" {
        // either textfield 1 or 2's text is empty
    }
}

Swift 2.0:

<强>保护

guard let text = descriptionLabel.text where !text.isEmpty else {
    return
}
text.characters.count  //do something if it's not empty

如果

if let text = descriptionLabel.text where !text.isEmpty
{
    //do something if it's not empty  
    text.characters.count  
}

Swift 3.0:

<强>保护

guard let text = descriptionLabel.text, !text.isEmpty else {
    return
}
text.characters.count  //do something if it's not empty

如果

if let text = descriptionLabel.text, !text.isEmpty
{
    //do something if it's not empty  
    text.characters.count  
}

答案 1 :(得分:48)

更好更美丽的使用

 @IBAction func Button(sender: AnyObject) {
    if textField1.text.isEmpty || textField2.text.isEmpty {

    }
}

答案 2 :(得分:11)

另一种检查实时textField源的方法:

 @IBOutlet var textField1 : UITextField = UITextField()

 override func viewDidLoad() 
 {
    ....
    self.textField1.addTarget(self, action: Selector("yourNameFunction:"), forControlEvents: UIControlEvents.EditingChanged)
 }

 func yourNameFunction(sender: UITextField) {

    if sender.text.isEmpty {
      // textfield is empty
    } else {
      // text field is not empty
    }
  }

答案 3 :(得分:5)

如果让......在...... {

Swift 3

if let _text = theTextField.text, _text.isEmpty {
    // _text is not empty here
}

Swift 2

if let theText = theTextField.text where !theTextField.text!.isEmpty {
    // theText is not empty here
}

警卫......在哪里......其他{

您还可以使用关键字guard

Swift 3

guard let theText = theTextField.text where theText.isEmpty else {
    // theText is empty
    return // or throw
}

// you can use theText outside the guard scope !
print("user wrote \(theText)")

Swift 2

guard let theText = theTextField.text where !theTextField.text!.isEmpty else {
    // the text is empty
    return
}

// you can use theText outside the guard scope !
print("user wrote \(theText)")

这对于验证链来说特别好,例如在表单中。您可以为每个验证编写guard let,如果出现严重错误,则返回或抛出异常。

答案 4 :(得分:5)

现在在swift 3 / xcode 8中,text属性是可选的,你可以这样做:

if ((textField.text ?? "").isEmpty) {
    // is empty
}

或:

if (textField.text?.isEmpty ?? true) {
    // is empty
}

或者,您可以进行如下所示的扩展,并改为使用它:

extension UITextField {
    var isEmpty: Bool {
        return text?.isEmpty ?? true
    }
}

...

if (textField.isEmpty) {
    // is empty
}

答案 5 :(得分:4)

Swift 2 / Xcode 7的小巧宝石

@IBAction func SubmitAgeButton(sender: AnyObject) {

    let newAge = String(inputField.text!)        

if ((textField.text?.isEmpty) != false) {
        label.text = "Enter a number!"
    }
    else {
        label.text = "Oh, you're \(newAge)"

        return
    }

    }

答案 6 :(得分:3)

也许我有点太晚了,但我们不能这样检查:

   @IBAction func Button(sender: AnyObject) {
       if textField1.text.utf16Count == 0 || textField2.text.utf16Count == 0 {

       }
    }

答案 7 :(得分:2)

好的,这可能会迟到,但在Xcode 8中我有一个解决方案:

t3

答案 8 :(得分:2)

使用此扩展名

extension String {
    func isBlankOrEmpty() -> Bool {

      // Check empty string
      if self.isEmpty {
          return true
      }
      // Trim and check empty string
      return (self.trimmingCharacters(in: .whitespaces) == "")
   }
}

像这样

// Disable the Save button if the text field is empty.
let text = nameTextField.text ?? ""
saveButton.isEnabled = !text.isBlankOrEmpty()

答案 9 :(得分:1)

雨燕4 / xcode 9

IBAction func button(_ sender: UIButton) {
        if (textField1.text?.isEmpty)! || (textfield2.text?.isEmpty)!{
                ..............
        }
}

答案 10 :(得分:0)

我只是想用简单的代码向您展示解决方案

@IBAction func Button(sender : AnyObject) {
 if textField1.text != "" {
   // either textfield 1 is not empty then do this task
 }else{
   //show error here that textfield1 is empty
 }
}

答案 11 :(得分:0)

现在已经太晚了,它在Xcode 7.3.1中工作正常

if _txtfield1.text!.isEmpty || _txtfield2.text!.isEmpty {
        //is empty
    }

答案 12 :(得分:0)

我使用了UIKeyInput内置功能hasTextdocs

对于Swift 2.3,我不得不将它用作方法而不是属性(因为它在文档中引用):

if textField1.hasText() && textField2.hasText() {
    // both textfields have some text
}

答案 13 :(得分:0)

Swif t 4.x解决方案

@IBOutlet var yourTextField: UITextField!

 override func viewDidLoad() {
     ....
     yourTextField.addTarget(self, action: #selector(actionTextFieldIsEditingChanged), for: UIControlEvents.editingChanged)
  }

 @objc func actionTextFieldIsEditingChanged(sender: UITextField) {
     if sender.text.isEmpty {
       // textfield is empty
     } else {
       // text field is not empty
     }
  }

答案 14 :(得分:0)

Swift 4.2

您可以为每个textField使用通用功能,只需在基本控制器中添加以下功能

// White space validation.
func checkTextFieldIsNotEmpty(text:String) -> Bool
{
    if (text.trimmingCharacters(in: .whitespaces).isEmpty)
    {
        return false

    }else{
        return true
    }
}

答案 15 :(得分:-1)

简单的检查方法

if TextField.stringValue.isEmpty {

}