tags:

views:

40

answers:

2

Hello,

I have a button and when its clicked/tapped I want to check that a text box has a value. If no value is present I want an alert to pop up. The button loads a second ViewController which displays waypoints on a google map, if no option is in the text box the google maps ViewController will have problems loading the waypoints as it wont know which objects to request from the database.

I've been playing around with the code below:

-(IBAction)mapButton
{
    if (route.text == NULL)
    //if (route.text == [NSNull null])  
    //if ([route.text isEqual:[NSNull null]])
    {
        mapViewController .sharedMapID=mapID;
        [self presentModalViewController:mapViewController animated:YES];       
    }
    else 
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Please select a route !" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show]; 
        [alert release];
    }

}
+1  A: 

Try this:

if (route.text.length == 0)
{
     // empty string or nil string
}
Shaji
A: 

Firstly, isn't that if the wrong way around? I thought you said that you wanted the UIAlert to appear if there was no text, not if it there was (route.text == null)...?

Secondly, have you tried just checking for an empty string (route.text.length == 0) instead of null? (Disclaimer - no idea about iPhone development...)

Stephen
yes, the if is the wrong way around, I was doing some 'Not Equal' to checking and forgot to change it.
Stephen
problem solved, I'm such an idiot, this line solved it: if ([route.text isEqualToString:@""]) as soon as swapped the conditions around.
Stephen
Wow, iPhone development has weird syntax. If you got the idea for checking for an empty string from either Shaji or I, would you mind marking one of us (probably him, he was first) as the correct answer? Helps the SO community know you've got your answer, thanks!
Stephen