我的 UIAlertView 不会在 if / else 构造中显示
我想确保用户在我的应用程序中弹出视图之前在文本字段中输入一些数据。这是我的代码:
if (nameField.text == NULL || lastNameField.text == NULL) {
UIAlertView *alertView= [[UIAlertView alloc] initWithTitle:@"Error" message:@"No
patient data specified, please specify name!" delegate:self
cancelButtonTitle:@"Okay" otherButtonTitles:nil];
[alertView show];
[alertView release];
问题是每次都会调用 else 语句,即使我故意将 textField
留空。
建议?
我也尝试扭转局面,将 else 部分放在 if 语句中,将要求更改为 != NULL
,但这也不起作用
I want to make sure that the user enters some data in a textfield before a view pops up in my app. Here is my code:
if (nameField.text == NULL || lastNameField.text == NULL) {
UIAlertView *alertView= [[UIAlertView alloc] initWithTitle:@"Error" message:@"No
patient data specified, please specify name!" delegate:self
cancelButtonTitle:@"Okay" otherButtonTitles:nil];
[alertView show];
[alertView release];
The problem is the else statement is being called every time, even when I deliberately leave the textField
blank.
Suggestions?
I also tried to turn it around, putting the else part in the if statement, changing the requirements to != NULL
, but that doesn't work either
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题是检查它是否为NULL。文本字段永远不会为 NULL。 NULL表示不存在。 textField 确实存在,但您真正想要检查的是该字段中是否有文本。方法是
nameField.length > 0 。所以你的代码应该是:
Your problem is checking if it is NULL. The textfield will never be NULL. NULL means it doesn't exist. The textField definatly exists, but what you actually want to check is if there is text in the field. The way to do that is
nameField.length > 0
. So your code should be: