Logic 我的UIAlertView不会显示在if/else构造中
我想确保在我的应用程序中弹出视图之前,用户在文本字段中输入一些数据。这是我的密码:Logic 我的UIAlertView不会显示在if/else构造中,logic,uialertview,if-statement,Logic,Uialertview,If Statement,我想确保在我的应用程序中弹出视图之前,用户在文本字段中输入一些数据。这是我的密码: if (nameField.text == NULL || lastNameField.text == NULL) { UIAlertView *alertView= [[UIAlertView alloc] initWithTitle:@"Error" message:@"No patient data
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表示它不存在。textField确实存在,但您实际需要检查的是字段中是否有文本。方法是nameField.length>0
。因此,您的代码应该是:
if (nameField.length == 0 || lastNameField.length == 0) {
UIAlertView *alertView= [[UIAlertView alloc] initWithTitle:@"Error" message:@"No patient data specified, please specify name!" delegate:self
cancelButtonTitle:@"Okay" otherButtonTitles:nil];
[alertView show];
[alertView release];