objective-c - if else 语句不起作用!

标签 objective-c button if-statement

这段代码没有错误,但是当我点击按钮时,“if”语句中没有任何内容 作品!它不会崩溃或显示错误...顺便说一句,我正在使用 Xcode 开发 iphone 应用程序。

#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 isEqualToString:@"Hello There!"]

关于objective-c - if else 语句不起作用!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1094031/

相关文章:

swift - Swift 中包含 "or"的 if 语句

iphone - 在 iOS 中存储日常数据

java - Action 事件停止

objective-c - 如何快速获得 1 位小数 float

java - Button2 正在做 Button1 应该做的事情

swift - 有没有办法每按下一个按钮 5 次就调用一个函数(Swift)?

python - 更简单的方法来编写这个坚持 pylint

javascript - 鼠标悬停图像卡在循环中

iphone - 为什么地址词典中没有邮政编码?

objective-c - iOS 如何处理数据库更新所需的时间