如果其他声明不起作用!

时间:2009-07-07 18:35:04

标签: objective-c button if-statement

此代码中没有错误,但是当我在“if”语句中单击按钮时没有任何内容 作品!它不会崩溃或显示错误...顺便说一下我在iPhone上的Xcode工作。

#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

4 个答案:

答案 0 :(得分:19)

您无法将字符串与==进行比较。只有当它们是完全相同的NSString对象时才会起作用,而不是它们是两个相同的字符串。使用[buttonLabel.text isEqualToString:@"Hello There!"]

答案 1 :(得分:8)

当你写:

Button2Label.text == @"Hello There!"

您正在测试按钮标签和静态字符串之间的指针相等性。您希望在此处测试字符串相等性,而不是指针相等:

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

也就是说,根据按钮的标签在动作方法中做出运行时决定是一个糟糕的设计,如果按钮的标签因任何原因而发生变化(包括本地化),将会给您带来麻烦。

关闭发件人或发件人的标签是首选模式。

答案 2 :(得分:2)

您不能使用==来比较字符串,它只是比较指针。

尝试:

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

答案 3 :(得分:0)

很可能if条件失败,试着否定你的条件来测试这个理论。如果它突然开始运行,请尝试在标签上调出text属性的值。

相关问题