如何在单击按钮时启动 NSTimer?

发布于 2024-09-11 09:46:41 字数 107 浏览 3 评论 0原文

当用户单击按钮时如何启动 NSTimer?

编辑:我的代码可以工作,一切都可以,但是我把代码放在 NSTimer 代码上方隐藏按钮,当我把它放在 NSTimer 代码下方时,它工作了!

How can i start an NSTimer when a user clicks a button?

EDIT:i had the code working and everything, but i put the code to hide the button above the NSTimer code, and when i placed it below the NSTimer code it worked!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

看透却不说透 2024-09-18 09:46:42

启动计时器的代码行是 (在此处阅读文档) :

[NSTimer scheduledTimerWithTimeInterval:1.0
                                 target:self
                               selector:@selector(timerFired:)
                               userInfo:nil
                                repeats:nil];

这将在 1 秒后调用方法 timerFired:

- (void)timerFired:(NSTimer *)timer {
  NSLog(@"timer : %@ has gone off", timer);
}

即要从 a 触发此方法按钮,您需要在 .h 文件中使用此方法:

- (IBAction)buttonPressed:(id)sender;

并在 .m 文件中,像这样检测按钮按下情况:

- (IBAction)buttonPressed:(id)sender {
  NSLog(@"Button's been pressed!");
}

然后,在界面生成器中将按钮的“Toiuch Up Inside”操作连接到此方法。

The line of code to start a timer is (read the docs here) :

[NSTimer scheduledTimerWithTimeInterval:1.0
                                 target:self
                               selector:@selector(timerFired:)
                               userInfo:nil
                                repeats:nil];

This will call the method timerFired: after 1 second i.e.

- (void)timerFired:(NSTimer *)timer {
  NSLog(@"timer : %@ has gone off", timer);
}

To get this to trigger from a button, you need this method in your .h file :

- (IBAction)buttonPressed:(id)sender;

and in your .m file, detect the button press like this :

- (IBAction)buttonPressed:(id)sender {
  NSLog(@"Button's been pressed!");
}

Then, in interface builder connect the button's 'Toiuch Up Inside' action to this method.

奢华的一滴泪 2024-09-18 09:46:42

将其安排在按钮的 IBAction 中。检查 NSTimer 类参考 了解如何使用定时器。

Schedule it in the IBAction of the button. Check NSTimer Class Reference to know how to use timers.

℉服软 2024-09-18 09:46:42

您必须插入命令[NSTimer SchedulerWithTimeInterval:1.0
目标:自己
选择器:@selector(timerFired:)
用户信息:无
重复:nil];

在按钮内单击。然后只有你可以让 NStimer 在按钮单击后运行(如果你想在按钮单击事件后运行计时器)

You have to insert the command of[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(timerFired:)
userInfo:nil
repeats:nil];

inside the buttonclick. Then only you could make the NStimer run after the button click (if you want to run the timer after the button click event )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文