开发者

My UIAlertView does not get displayed in if / else construction

开发者 https://www.devze.com 2023-04-07 11:32 出处:网络
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:

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


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:

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];
0

精彩评论

暂无评论...
验证码 换一张
取 消