如何在 xcode 中延迟事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13436315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:07:43  来源:igfitidea点击:

How do I delay an event in xcode?

iphoneobjective-ciosxcode

提问by Herbie999

I am surprised I can't find this answer but for some reason can't find it for Xcode.

我很惊讶我找不到这个答案,但由于某种原因无法在 Xcode 中找到它。

In my app I have an IBAction buttonPressed and it adds coins to my "coins" variable and displays it in a UILabel. I would like to make it so when the user presses the button it does not add the coins or display it in the UILabel for about 30 seconds. Is there a simple way to do this? I'm pretty new, so please explain in simple steps if possible. Thank You :)

在我的应用程序中,我有一个 IBAction buttonPressed,它将硬币添加到我的“硬币”变量中,并将其显示在 UILabel 中。我想这样做,当用户按下按钮时,它不会添加硬币或在 UILabel 中显示大约 30 秒。有没有一种简单的方法可以做到这一点?我很新,所以如果可能的话,请用简单的步骤解释。谢谢你 :)

回答by rdelmar

It's very easy, just use performSelector:withObject:afterDelay:. You would put it in your IBAction code like this:

这很简单,只需使用 performSelector:withObject:afterDelay:。你可以像这样把它放在你的 IBAction 代码中:

-(IBAction)buttonPressed:(UIButton *) sender {
[self performSelector:@selector(addCoins) withObject:nil afterDelay:30];
}

-(void)addCoins {
//put whatever code you want to happen after the 30 seconds
}