iOS比较按钮标题到字符串 [英] iOS compare button title to string

查看:115
本文介绍了iOS比较按钮标题到字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何编码,所以感谢您对这个简单问题的耐心。

I'm just learning how to code so thanks for your patience on this simple question.

这是我的代码:

- (IBAction)buttonWasPressed:(id)sender {  
    NSString *buttonName = [sender titleForState:UIControlStateNormal];  
    if (buttonName == @"Button 1") {  
        do something 
    }

如何将作为发送者传递的按钮的标题与字符串进行比较?

How do I compare the title of the button passed as sender to a string?

非常感谢您的帮助。

推荐答案

在objective-c您无法使用==,
来比较字符串,而应使用NSString类中的方法 isEqualToString 来比较字符串与另一个字符串。

in objective-c you can't compare strings using "==", instead you should use the method isEqualToString from the NSString class to compare a string with another.

if ([buttonName isEqualToString: @"Button 1"]) {
  // do something
}

这篇关于iOS比较按钮标题到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆