Question

In a game... When the "done" button is pressed, there are 2 'if-statements': one brings you to the next level and the other returns you home. How can I make it so only one happens at a time? The way it works now, is both the winning and losing 'alertviews' pop up no matter if the user got it right or wrong. This is how it looks:

-(IBAction)done {

    if ([entry.text isEqualToString:sentance.text]) {

        UIAlertView *alert = [[UIAlertView alloc] 
                             initWithTitle:@"Cleared!" 
                             message:@""
                             delegate:nil 
                             cancelButtonTitle:@"Ok" 
                             otherButtonTitles: nil];
        [alert show];
        [alert release];

        seconds.text = @"5 Seconds";
    }

    if ([entry.text isEqualToString:entry.text]) {

        NSString *nssScore= [NSString stringWithFormat:@"Final Score: \n %i Points", Score];

        UIAlertView *alert = [[UIAlertView alloc]
                             initWithTitle:@"Nope! Wrong"
                             message:nssScore
                             delegate:nil
                             cancelButtonTitle:@"Ok"
                             otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
}
Was it helpful?

Solution 2

You can use else if condition.

Like this :

-(IBAction)done {

    if ([entry.text isEqualToString:sentance.text]) {

        UIAlertView *alert = [[UIAlertView alloc] 
                              initWithTitle:@"Cleared!" 
                              message:@""
                              delegate:nil 
                              cancelButtonTitle:@"Ok" 
                              otherButtonTitles: nil];
        [alert show];
        [alert release];

        seconds.text = @"5 Seconds";
    }
    else if ([entry.text isEqualToString:entry.text]) {

        NSString *nssScore= [NSString stringWithFormat:@"Final Score: \n %i Points", Score];

        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"Nope! Wrong"
                              message:nssScore
                              delegate:nil
                              cancelButtonTitle:@"Ok"
                              otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
}

or you can add a line of code in each block "return;"

like this:

OTHER TIPS

There is something wrong with your second if.

if ([entry.text isEqualToString:entry.text]) {

This if is always true, since why should entry.text not be equal to itself?

Also if you have two if statements and only one shall be true, you always use a else if for the second one:

if (<condition>) {

} else if (<other condition>) {

}

If condition is true, the other if is not executed, even if other condition is also true. However, in your case your second if seems nonsense to me. An if that is always true is useless and be left out completely. The code is equivalent to

if (<condition>) {

} else {

}

I got the answer guys...

    if ([a.text isEqualToString:b.text] == FALSE) {

condition

}

Thanks!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top