views:

360

answers:

4

This code has no errors in it, but when i click the button nothing in the "if" statement works! it doesn't crash or show errors... Btw im working in Xcode on an iphone app.

#import "MainView.h"

@implementation MainView

@synthesize MyButton2, MyMainTextLabel;
@synthesize MyButton3, MyMainTextLabel;
@synthesize MyButton2, Button2Label;
@synthesize MyButton2, Button3Label;
@synthesize MyButton3, Button2Label;
@synthesize MyButton3, Button3Label;


- (IBAction)Button2click {


    if(Button2Label.text == @"Hello There!") {

     MyMainTextLabel.text = @"\"Hey...!!\"";

     Button3Label.text = @"What a rude Penguin!";
     Button2Label.text = @"Hows a Goin?";
    }

}

- (IBAction)Button3click {

    if(Button3Label.text == @"Penguins SUCK!") {

     MyMainTextLabel.text = @"\"DONT TEST ME!!\"";

     Button3Label.text = @"Oh I Will!";
     Button2Label.text = @"Sorry I didnt mean to...";

    }
}

- (IBAction)buttonclick {

}
@end
A: 

Most likely the if condition is failing, try negating your condition to test that theory. If it suddenly starts running, try debuging the value of the text property on your labels.

Adam Luter
+19  A: 

You can't compare strings with ==. That will work only if they are the exact same NSString object, not if they are two identical strings. Use [buttonLabel.text isEqualToString:@"Hello There!"]

Chuck
Removing my answer because this one is better.
Paul Tomblin
+2  A: 

You can't use == to compare strings, it just compares the pointers.

Try:

if ([Button2Label.text compare:@"Hello There!"] == NSOrderedSame)

FigBug
+8  A: 

When you write:

Button2Label.text == @"Hello There!"

you are testing for pointer equality between the button's label and your static string. You want to test from string equality, not pointer equality here:

if ([Button2Label.text isEqualToString: @"Hello There!") { ... }

That said, making runtime decisions in your action methods based on the button's label is a poor design, and will run you into trouble if the button's label changes for any reason, including localization.

Switching off the sender, or the sender's tag, is the preferred pattern.

Jim Correia
+1. It's almost always better to use the tag value.
Chuck