如何测试 UITextField 是否为零?
我正在尝试制作我的应用程序的一部分,如果该人不更改我的 UITextField 中的空白文本,那么他/她将无法继续下一步。基本上,我想测试 UITextField 的 nil 文本。我已经使用了 if (text == @"") 方法,但是如果用户单击 UITextField 但没有键入,则 if 语句不起作用。由于某种原因,它不认为文本 == nil 或“”。我执行代码是否错误。任何其他选项。请帮忙!!!
I am trying to make a part of my app where if the person doesn't change the blank text in my UITextField, then he/she can't go on to the next step. Basically, I want to test the UITextField for nil text. I have used the if (text == @"") method, but if the person clicks on the UITextField but doesn't type, then the if statement doesn't work. For some reason it doesn't think the text == nil or "". Am I implementing the code wrong. Any other options. Please help!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该检查
text
属性的length
:You should be checking the
length
of thetext
property:这是我使用的类别...
这样一个 nil 字符串将计算为 false,这是正确的。创建
isBlank
会返回 false 表示 nil,这是不正确的。Here's the category I use...
This way a nil string would evaluate to false, which is correct. Creating an
isBlank
would return false for nil, which isn't correct.我已经编写代码来检查字符串是否为空。此代码还检查仅字符串空间,该空间对于商店名称和地址等也为空。这将对您有所帮助。
谢谢
I have write code to check the string is empty or not. This code also check for the string only space that is also empty for store name and address etc. this will help you.
Thanks
如果我是你,我会在用户打字时禁用和启用该按钮。恕我直言,当没有文本时按钮看起来被禁用比让用户单击按钮告诉他不允许他移动到下一个视图要好。大多数苹果自己的应用程序都是这样做的。
您可以通过使用 UITextFieldDelegate 方法 像这样
如果您提供预填充文本字段,您必须在 viewDidLoad 中(或任何您想要的地方)启用按钮,如果您提供空字段,则必须首先禁用它。
If I were you I would disable and enable the button while the user is typing. Imho it's better that the button looks disabled when there is no text than having the user click the button to tell him that he is not allowed to move to the next view. Most of apples own apps do it like this.
You achieve this behavior by using the UITextFieldDelegate method like this
If you provide a prefilled textfield you have to enable the button in viewDidLoad (or where ever you want) and if you provide an empty field you have to disable it initally.