0

每个人!

所以基本上,我试图从我的 UIButton 中创建一个简单的切换。使用下面的代码,我可以单击该按钮,但只有很短的时间我才能看到 @"Expense" 作为标题。这可能是一个非常简单的错误。任何帮助表示赞赏!

- (IBAction)typeChanger {
if ([typeButton.titleLabel.text isEqual:@"Income"]) {
    typeButton.titleLabel.text = @"Expense";
}else if ([typeButton.titleLabel.text isEqual:@"Expense"]) {
    typeButton.titleLabel.text = @"Income";
}
}

提前致谢!

4

3 回答 3

2

Do like this,

- (IBAction)typeChanger {
if ([typeButton.titleLabel.text isEqual:@"Income"]) {
   [typeButton setTitle:@"Expense" forState:UIControlStateNormal];
}else if ([typeButton.titleLabel.text isEqual:@"Expense"]) {
    [typeButton setTitle:@"Income" forState:UIControlStateNormal];
}
}
于 2013-02-26T04:40:20.233 回答
2

This may be very useful and handy. Try this out.

https://github.com/Brayden/UICheckbox

于 2013-02-26T06:40:54.163 回答
0

您使用 NSString 的isEqualToString:方法而不是isEqual字符串比较(两者都应该工作,但根据文档它更快)。

此外,您需要使用setTitle:forState:来设置 a ... 的标题,要设置UIButton的两个重要状态是UIControlStateNormal(未按下)和UIControlStateHighlighted(按下时):

- (IBAction)typeChanger {
    if ([[typeButton titleForState:UIControlStateNormal] isEqualToString:@"Income"]) {
         [typeButton setTitle:@"Expense" forState:UIControlStateNormal];
         [typeButton setTitle:@"Expense" forState:UIControlStateHighlighted];

    } else if ([[typeButton titleForState:UIControlStateNormal] isEqualToString:@"Expense"]) {
         [typeButton setTitle:@"Income" forState:UIControlStateNormal];
         [typeButton setTitle:@"Income" forState:UIControlStateHighlighted];
    }
}

另请参阅以下文档NSString

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

And on UIButton:

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIButton_Class/UIButton/UIButton.html

于 2013-02-26T04:31:21.353 回答