Objective c 如果else语句不起作用!

Objective c 如果else语句不起作用!,objective-c,button,if-statement,Objective C,Button,If Statement,这段代码中没有错误,但是当我单击按钮时,“if”语句中没有任何内容 作品它不会崩溃或显示错误。。。顺便说一句,我在iphone应用程序的Xcode中工作 #import "MainView.h" @implementation MainView @synthesize MyButton2, MyMainTextLabel; @synthesize MyButton3, MyMainTextLabel; @synthesize MyButton2, Button2Label; @synthes

这段代码中没有错误,但是当我单击按钮时,“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

最有可能的情况是,如果条件是失败的,尝试否定你的条件,以测试该理论。如果它突然开始运行,请尝试调试标签上文本属性的值。

您无法将字符串与
=
进行比较。只有当它们是完全相同的NSString对象时,这才有效,而不是当它们是两个相同的字符串时。使用
[buttonLabel.text IsequalString:@“您好!”

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

尝试:

if([Button2Label.text compare:@“你好!”]==sensorderedname)

当您编写:

Button2Label.text=@“你好!”

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

if([Button2Label.text IsequalString:@“你好!”){…}

这就是说,根据按钮的标签在操作方法中做出运行时决策是一种糟糕的设计,如果按钮的标签因任何原因(包括本地化)发生更改,将给您带来麻烦


关闭发送者或发送者标记是首选模式。

如果没有错误,它将正常工作。:)你应该更具体地说编译器错误。这不是答案,只是一个会减少混淆的问题:通用的Objective-C约定是用大写字母写类名,而方法和变量名以小写字母开头,以驼峰大小写。因此方法名应该是“button3Click:”,变量应该是“button2Label”。从这里的书写方式来看,button2Label似乎是一个类。删除我的答案,因为这个更好。