禁用和延迟IBACTION按钮
-
29-09-2019 - |
题
我有一个iBaction按钮,我想在延迟30秒后启用。该按钮将在视图中,但禁用了30秒。
有人知道我该怎么做吗?
这是我所拥有的 - 简单的ibaction播放一些音频:
-(IBAction) playSound:(id)sender {
[theAudio play];
}
解决方案
您可以使用以下方式:
- (IBAction)playSound:(id)sender
{
[theAudio play];
UIButton *theButton = (UIButton *) sender;
theButton.enabled = NO;
[self performSelector:@selector(enableButton:) withObject:theButton afterDelay:30.0];
}
- (void)enableButton:(UIButton *)button
{
button.enabled = YES;
}
假设按下按钮时要禁用该按钮。
其他提示
在ViewDidload或您想要的其他合适方法中:
[myButton setUserInteractionEnabled:FALSE]; [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(enableButton:) userInfo:nil repeats:NO];
然后,
- (void)enableButton:(NSTimer *)timer { [myButton setUserInteractionEnabled:TRUE]; }
注意:我尚未编译代码,刚刚写道。可能有错字。
不隶属于 StackOverflow