根据标签值禁用按钮
我有一个名为 label
的 UILabel
,您可以使用两个按钮对其加或减 1。当你一直减到 0 时,我希望减号按钮停止工作。如果添加了该值,我希望减号按钮再次起作用。这是我用于加/减按钮的方法/代码:
- (IBAction)addButton1:(id)sender {
[label setText:[NSString stringWithFormat:@"%d",[label.text intValue] +1]];
}
加/减方法的代码是相同的。除了最后的+1 是-1。
我尝试过:
- (IBAction)addButton1:(id)sender {
int val = [label.text intValue];
[label setText:[NSString stringWithFormat:@"%d",[label.text intValue] +1]];
if(val - 1 <= 0) {
UIButton *button = (UIButton *)sender;
[button setEnabled:NO];
}
}
I have a UILabel
called label
, and you can add or subtract 1 from it using two buttons. When you subtract all the way down to 0, I want the minus button to stop working. And if the value is added, I want the minus button to work again. Here is the method/code I'm using for the add/subtract button:
- (IBAction)addButton1:(id)sender {
[label setText:[NSString stringWithFormat:@"%d",[label.text intValue] +1]];
}
the code is the same for both the add/subtract methods. Except the +1 at the end is a -1.
I tried:
- (IBAction)addButton1:(id)sender {
int val = [label.text intValue];
[label setText:[NSString stringWithFormat:@"%d",[label.text intValue] +1]];
if(val - 1 <= 0) {
UIButton *button = (UIButton *)sender;
[button setEnabled:NO];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
您只需将指针保持在减号按钮上(只需创建一个
IBOutlet
,然后使用IB将其链接到按钮)Try
You simply need to keep the pointer to the minus button (simply create an
IBOutlet
and then link it to the button using IB)