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

问题描述 投票:49回答:15

我正在研究下面的代码,检查textField1textField2文本字段是否有任何输入。

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

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

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

  //then do something

      }  
  }
swift textbox
15个回答
157
投票

简单地将textfield对象与空字符串""进行比较并不是正确的方法。您必须比较textfield的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  
}

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

0
投票

现在已经太晚了,它在Xcode 7.3.1中运行良好

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

0
投票

我使用了UIKeyInput的内置功能hasTextdocs

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

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

0
投票

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

0
投票

Swift 4.2

您可以为每个textField使用常规函数,只需在基本控制器中添加以下函数即可

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

    }else{
        return true
    }
}

-1
投票

简单的检查方式

if TextField.stringValue.isEmpty {

}

48
投票

使用更美好,更美观

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

    }
}

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

5
投票

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

斯威夫特3:

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

斯威夫特2:

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

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

您还可以使用关键字guard

斯威夫特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)")

斯威夫特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,如果出现严重错误则返回或抛出异常。


5
投票

就像现在swift 3 / xcode 8文本属性是可选的,你可以这样做:

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
}

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
    }

    }

3
投票

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

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

       }
    }

2
投票

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

if(textbox.stringValue.isEmpty) {
    // some code
} else {
    //some code
}

1
投票

Swift 4/xcode 9

IBAction func button(_ sender: UIButton) {
        if (textField1.text?.isEmpty)! || (textfield2.text?.isEmpty)!{
                ..............
        }
}
© www.soinside.com 2019 - 2024. All rights reserved.