-2

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];
    }
}
Boris Prohaska
  • 912
  • 6
  • 16
  • Depending on how may `if's` you have left, you could either say `else if ([entry.text isEqualToString:entry.text])` or just instead of the second if type `else` and delete the whole statement – Boris Prohaska Jan 28 '13 at 14:19
  • If you're having trouble formatting a couple of conditionals, I think you should really look into reading a book about the basics of programming. If you really want to dig into Objective-C specifically, I'd recommend you read [Cocoa and Objective-C: Up and Running](http://www.amazon.com/Cocoa-Objective-C-Up-Running-ebook/dp/B0043EWUCU/ref=sr_1_1?ie=UTF8&qid=1370104091&sr=8-1&keywords=up+and+running+cocoa). – Imirak Jun 01 '13 at 16:29

3 Answers3

3

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 {

}
Mecki
  • 125,244
  • 33
  • 244
  • 253
  • So is there anything I can write that basically says the first 'If' means you got it correct, and the second 'If' is anything except the correct answer..? – Zachary Rosenberg Jan 28 '13 at 14:41
  • Yes, "if - else if" does exactly that. If the first if is true, the second if is not even looked at. But again, what are you trying to test in your second if? The second if is always true, as it tests a value with itself. – Mecki Jan 28 '13 at 15:45
0

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:

Michael Celey
  • 12,645
  • 6
  • 57
  • 62
Bharath
  • 314
  • 2
  • 8
-1

I got the answer guys...

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

condition

}

Thanks!