iOS:将按下的按钮与标签文本进行比较
我有一个简单的函数来查看触摸的按钮是否与标签的文本相同:
- (IBAction) checkIt:(id)sender{
UIButton *button = (UIButton *)sender;
if(button.getText() == randomNumber.text){
randomNumber.text = @"Nice.";
}
else{
randomNumber.text = @"Try Again";
}
}
其中“randomNumber”是一个标签。然而,这不起作用。我是 Cocoa/Objective-C 初学者,我不确定正确的语法是什么。
我愿意接受您想要/认为对像我这样的白痴有帮助的任何附加信息。 :)
四个按钮映射到此功能:牛、猪、青蛙、羊。 “randomNumber”标签是从字符串“Cow,Frog”数组中随机生成的...
I have a simple function to see if the button touched is the same as the text of a label:
- (IBAction) checkIt:(id)sender{
UIButton *button = (UIButton *)sender;
if(button.getText() == randomNumber.text){
randomNumber.text = @"Nice.";
}
else{
randomNumber.text = @"Try Again";
}
}
Where "randomNumber" is a label. However, this doesn't work. I'm a Cocoa/Objective-C beginner and I'm not sure what the proper syntax is.
I'm open to any addition information you would like/think is helpful to idiots like me. :)
Four buttons are mapped to this function: Cow, Pig, Frog, Sheep. The "randomNumber" label is randomized from an array of strings "Cow,Frog"...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这永远不会起作用,因为这不是比较字符串的正确方法。 '==' 只比较内存地址,NSString 方法 isEqualToString 实际上比较字符串。
另外,我将使用属性 titleLabel.text 获取按钮文本。所以,我会尝试这样:
This will never work because this is not the right way to compare strings. '==' only compares the memory address, the NSString method
isEqualToString
actually compares the strings.Also, I would get the button text by using the property, titleLabel.text. So, I would try like this: