【问题标题】:Disabling and Enabling a button on event iOS [duplicate]在事件iOS上禁用和启用按钮[重复]
【发布时间】:2016-03-20 03:05:42
【问题描述】:

我有一个停止时钟应用程序,希望在最初按下 startCountButton 按钮时禁用它,然后在按下 stopCountButton 按钮后再次启用它,以便只能按下开始按钮一次。这是我的代码

- (IBAction)startCount:(UIButton*)sender {
    countInt = 0;
    self.label.text = [NSString stringWithFormat:@"%i", countInt];
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countTimer) userInfo:nil repeats:YES];
}

- (IBAction)stopCount:(UIButton*)sender {
    countInt = 0;
    [timer invalidate];
}

-(void)countTimer {
    countInt += 1;
    self.label.text = [NSString stringWithFormat:@"%i", countInt];
}

对我需要添加的内容有什么帮助吗?我不想更改文本,只是禁用它

【问题讨论】:

  • 你有什么问题?是什么阻止了你做你所说的?

标签: ios objective-c


【解决方案1】:

首先,您需要对按钮的引用。然后,将以下代码添加到startCount::

((UIButton *)sender).enabled = NO

并在stopCount: 中添加:

startCountButton.enabled = YES

【讨论】:

  • 当我添加这两行时,我收到以下错误消息:Use of undeclared identifier 'startCount'
  • @peterbonar 什么?这两行中没有startCount 这个词。你加了什么?
  • 按钮不叫startButton
  • 是的,按钮也叫startCount
  • 我上面已经说过会产生错误信息
【解决方案2】:

startCount: 和 stopCount: 都接受 UIButton 作为它们的参数,但我对如何调用第二个参数感到困惑。

如果startCount:被你想禁用的按钮调用,你可以这样写:

sender.enabled = NO;

但是stopCount: 很棘手,因为很明显它不能被按钮调用,因为它刚才被禁用了。如果stopCount: 是从另一个按钮调用的(我猜它一定是这样),那么您必须存储对第一个按钮的引用才能重新启用它。然后你可以这样做:

self.disabledButton.enabled = YES;

【讨论】:

  • enabled 更好,因为(取决于使用的按钮类型)它也会淡出按钮。
  • 哈!我只是在做那个确切的改变:)
【解决方案3】:

使用enable 属性,您可以启用/禁用按钮:

startCountButton.enabled = NO

【讨论】:

  • 问题被标记为 Objective-C,而不是 Swift。请以适当的语言发布答案。
猜你喜欢
  • 1970-01-01
  • 2011-12-21
  • 1970-01-01
  • 2018-07-15
  • 2012-09-20
  • 2014-09-27
  • 2023-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多