我如何使用 NSTimer(或其他)使事件在 Cocoa 中的特定时间之后发生?
我正在使用 Xcode 创建一个用 Objective-C 编写的 Mac OSX Cocoa 应用程序。
这是我的代码:
-(IBAction)clickToLaunchAppButtonClicked:(id)sender; {
//I want to hide the click to load app button
[clickToLoadButton setEnabled:NO];
[clickToLoadButton setHidden:YES];
//***I want to make the app wait for 1 second (This is where I'm stuck!)***
//I want to show the loading label
[loadingLabel setEnabled:YES];
[loadingLabel setHidden:NO];
//I want to show the loading progress bar and initialize it
[loadingProgressBar setHidden:NO];
[loadingProgressBar startAnimation:self];
//***I want to make the app wait for 3 seconds (Again, I don't know how to do this!)***
//I want to stop the loading progress bar animation
[loadingProgressBar stopAnimation:self];
//I want to hide the loading progress bar and loading label
[loadingLabel setHidden:YES];
[loadingLabel setEnabled:NO];
[loadingLabel setHidden:YES];
}
如何让应用程序等待/暂停几秒钟?我尝试了 wait()
、delay()
或 pause()
函数,但它们冻结了应用程序,这不是我想要的。我应该使用 NSTimer
吗?如果是这样,请给我一个简单的实现方法。
I am using Xcode to create a Cocoa app for Mac OSX written in Objective-C.
Here is my code:
-(IBAction)clickToLaunchAppButtonClicked:(id)sender; {
//I want to hide the click to load app button
[clickToLoadButton setEnabled:NO];
[clickToLoadButton setHidden:YES];
//***I want to make the app wait for 1 second (This is where I'm stuck!)***
//I want to show the loading label
[loadingLabel setEnabled:YES];
[loadingLabel setHidden:NO];
//I want to show the loading progress bar and initialize it
[loadingProgressBar setHidden:NO];
[loadingProgressBar startAnimation:self];
//***I want to make the app wait for 3 seconds (Again, I don't know how to do this!)***
//I want to stop the loading progress bar animation
[loadingProgressBar stopAnimation:self];
//I want to hide the loading progress bar and loading label
[loadingLabel setHidden:YES];
[loadingLabel setEnabled:NO];
[loadingLabel setHidden:YES];
}
How can I make the app wait/pause for a few seconds? I tried the wait()
, delay()
or pause()
functions, but they freeze the app, which is not what I want. Should I use an NSTimer
? If so, please give me a simple way to implement it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议改用新的 Grand Central Dispatch API。
例如:
有三个优点:
I recommend using the new Grand Central Dispatch API instead.
For example:
There are three advantages:
您可以将要在 3 秒延迟上运行的代码放在另一个方法 (delayedLoad) 中,并使用
performSelector: withObject: afterDelay:
编辑 - 详细拼写以响应您的评论
以创建另一个方法,将此代码包含在同一个 .m 文件(AppDelegate.m 或类似文件)中
,并且您现有的方法将变为
这就是您实现 3 秒延迟的方式。希望你可以扩展它以实现 1 秒延迟
You can place the code you want to run on the 3 second delay in another method (delayedLoad) and use
performSelector: withObject: afterDelay:
EDIT - spelling it out more in response to your comment
to create the other method, include this code in the same .m file (AppDelegate.m or similar)
And your existing method becomes
This is how you implement the 3 second delay. Hopefully you can extend this to implement the 1 second delay