我正在处理下面的代码,检查textField1和textField2文本字段是否有任何输入.
IF当我按下按钮时,声明没有做任何事情.
@IBOutlet var textField1 : UITextField = UITextField()
@IBOutlet var textField2 : UITextField = UITextField()
@IBAction func Button(sender : AnyObject)
{
if textField1 == "" || textField2 == ""
{
//then do something
}
}
Run Code Online (Sandbox Code Playgroud)
Bri*_*acy 158
简单地将textfield 对象与空字符串进行""比较并不是解决此问题的正确方法.您必须比较textfield的text属性,因为它是兼容类型并保存您要查找的信息.
@IBAction func Button(sender: AnyObject) {
if textField1.text == "" || textField2.text == "" {
// either textfield 1 or 2's text is empty
}
}
Run Code Online (Sandbox Code Playgroud)
Swift 2.0:
警卫:
guard let text = descriptionLabel.text where !text.isEmpty else {
return
}
text.characters.count //do something if it's not empty
Run Code Online (Sandbox Code Playgroud)
如果:
if let text = descriptionLabel.text where !text.isEmpty
{
//do something if it's not empty
text.characters.count
}
Run Code Online (Sandbox Code Playgroud)
Swift 3.0:
警卫:
guard let text = descriptionLabel.text, !text.isEmpty else {
return
}
text.characters.count //do something if it's not empty
Run Code Online (Sandbox Code Playgroud)
如果:
if let text = descriptionLabel.text, !text.isEmpty
{
//do something if it's not empty
text.characters.count
}
Run Code Online (Sandbox Code Playgroud)
UnR*_*ewa 48
使用更美好,更美观
@IBAction func Button(sender: AnyObject) {
if textField1.text.isEmpty || textField2.text.isEmpty {
}
}
Run Code Online (Sandbox Code Playgroud)
rap*_*ael 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
}
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特3:
if let _text = theTextField.text, _text.isEmpty {
// _text is not empty here
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特2:
if let theText = theTextField.text where !theTextField.text!.isEmpty {
// theText is not empty here
}
Run Code Online (Sandbox Code Playgroud)
您还可以使用关键字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)")
Run Code Online (Sandbox Code Playgroud)
斯威夫特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)")
Run Code Online (Sandbox Code Playgroud)
这对于验证链来说尤其有用,例如在表单中.guard let如果出现严重错误,您可以为每个验证编写一个并返回或抛出异常.
就像现在swift 3/xcode 8文本属性是可选的,你可以这样做:
if ((textField.text ?? "").isEmpty) {
// is empty
}
Run Code Online (Sandbox Code Playgroud)
要么:
if (textField.text?.isEmpty ?? true) {
// is empty
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以进行如下所示的扩展,并使用它:
extension UITextField {
var isEmpty: Bool {
return text?.isEmpty ?? true
}
}
...
if (textField.isEmpty) {
// is empty
}
Run Code Online (Sandbox Code Playgroud)
使用这个扩展
extension String {
func isBlankOrEmpty() -> Bool {
// Check empty string
if self.isEmpty {
return true
}
// Trim and check empty string
return (self.trimmingCharacters(in: .whitespaces) == "")
}
}
Run Code Online (Sandbox Code Playgroud)
像这样
// Disable the Save button if the text field is empty.
let text = nameTextField.text ?? ""
saveButton.isEnabled = !text.isBlankOrEmpty()
Run Code Online (Sandbox Code Playgroud)