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];
    }
}
Était-ce utile?

La 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:

Autres conseils

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!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top