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
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!"]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With