我试图触发模态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") }更多推荐
发布评论