通过text.isEmpty确定空文本字段(Determine Empty Text Field via text.isEmpty)

编程入门 行业动态 更新时间:2024-10-28 04:21:07
通过text.isEmpty确定空文本字段(Determine Empty Text Field via text.isEmpty)

我试图触发模态segue如果文本字段为空,我有以下代码:

@IBAction func createUserButtonPressed(sender: UIButton) { if self.emailAddressTextField.text.isEmpty{ func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){ if (segue.identifier == "ShowErrorSegue"){ } } } }

emailAddressTextField是文本字段,我想要学习的是,如果用户尝试在没有电子邮件地址的应用中创建帐户,则应该弹出一个弹出窗口。

即使文本字段为空,问题是(self.emailAddressTextField.text.isEmpty)也不返回true。 我在这里想念的是什么?

请建议。 谢谢。

I am trying to trigger a modal segue if the text field is empty for which I have following code:

@IBAction func createUserButtonPressed(sender: UIButton) { if self.emailAddressTextField.text.isEmpty{ func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){ if (segue.identifier == "ShowErrorSegue"){ } } } }

emailAddressTextField is the text field and what I am trying to learn to do is that user should get a popup if they try to create an account in my app without email address.

The problem is (self.emailAddressTextField.text.isEmpty) doesn't return true even though the text field is empty. What am I missing here?

Please suggest. Thanks.

最满意答案

您可以只计算文本字段元素,也可以使用stringByTrimmingCharactersInSet方法删除任何空格:

if countElements(emailAddressTextField.text.stringByTrimmingCharactersInSet(.whitespaceCharacterSet())) > 0 { println("not empty") } else { println("empty") }

You can just count your text field elements and you can also get rid of any spaces using stringByTrimmingCharactersInSet method:

if countElements(emailAddressTextField.text.stringByTrimmingCharactersInSet(.whitespaceCharacterSet())) > 0 { println("not empty") } else { println("empty") }

更多推荐

本文发布于:2023-08-07 15:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464680.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   文本   isEmpty   text   Field

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!