NStimer 和 for 循环

发布于 2024-10-18 03:58:46 字数 569 浏览 2 评论 0原文

我正在为此苦苦挣扎。我看到一些代码可以执行此操作:

- (void)startTimer {
    pauseTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(doActions) userInfo:nil repeats:YES];
} 

然后在 doActions 中调用它。

问题是我想在按下按钮时调用它,并且执行操作是一个IBaction。我不断收到 sigAbrt

有人可以给我一些示例代码,您可以在按下按钮时每 1 秒将标签从“开”更改为“关”吗?

编辑

我的意思是如果 doActions 看起来像这样

- (IBAction) doActions {
for(int j; j<100; j++){

theLabel.hidden != theLabel.hidden;
//Does invalidate timer go here?
}
}

I'm struggling with this. I saw some code where you can do this :

- (void)startTimer {
    pauseTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(doActions) userInfo:nil repeats:YES];
} 

Then call it in doActions.

Problem is i want to call it while a button is being pressed, and do actions is an IBaction. I keep getting a sigAbrt.

Can someone give me some sample code where you cay change a label from 'on' to 'off' every 1 second while a button is being pressed?

EDIT

i mean if doActions looks like this

- (IBAction) doActions {
for(int j; j<100; j++){

theLabel.hidden != theLabel.hidden;
//Does invalidate timer go here?
}
}

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

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

发布评论

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

评论(2

一页 2024-10-25 03:58:46

我仍然不清楚你想要实现什么:毕竟,如果你只是用简单的英语写下来的话,我会发现它会容易得多。

也就是说,我认为这将帮助您到达您想去的地方:

// Assuming ivars:
// NSTimer* toggleTimer
// NSUInteger toggleCount

- (void)toggleTimerFired:(NSTimer*)timer
{
  if ( toggleCount++ >= 100 ) {
    [self stopToggling:self];
    return;
  }
  // do whatever you need to do a hundred times here
}

- (IBAction)stopToggling:(id)sender
{
  [toggleTimer invalidate], toggleTimer = nil;  // you don't want dangling pointers...
  // perform any other needed house-keeping here
}

- (IBAction)startToggling:(id)sender
{
  [self stopToggling:self]; // if `startToggling:` will NEVER be called when a timer exists, this line CAN be omitted.
  toggleCount = 0;
  toggleTimer = [NSTimer scheduledTimerWithTimeInterval:1. target:self selector:@selector(toggleTimerFired:) userInfo:nil repeats:YES];
}

根据您想要执行的操作,startToggling: 需要在 touchUpInsidetouchDown 上发送。在第二种情况下,可能需要在按钮的任何 touchUp... 事件上调用 stopToggling:

It still isn't clear to me, what you're trying to accomplish: I would have found it much easier, if you simply had it written down in plain english, after all.

That said, here's what I think will get you where you want to go:

// Assuming ivars:
// NSTimer* toggleTimer
// NSUInteger toggleCount

- (void)toggleTimerFired:(NSTimer*)timer
{
  if ( toggleCount++ >= 100 ) {
    [self stopToggling:self];
    return;
  }
  // do whatever you need to do a hundred times here
}

- (IBAction)stopToggling:(id)sender
{
  [toggleTimer invalidate], toggleTimer = nil;  // you don't want dangling pointers...
  // perform any other needed house-keeping here
}

- (IBAction)startToggling:(id)sender
{
  [self stopToggling:self]; // if `startToggling:` will NEVER be called when a timer exists, this line CAN be omitted.
  toggleCount = 0;
  toggleTimer = [NSTimer scheduledTimerWithTimeInterval:1. target:self selector:@selector(toggleTimerFired:) userInfo:nil repeats:YES];
}

Depending on what exactly you want to do, startToggling: needs to be sent on either touchUpInside or touchDown. In the second case, stopToggling: probably needs to be called on any touchUp... event of the button.

累赘 2024-10-25 03:58:46
- (void)startTimer {
    pauseTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(doActions) userInfo:nil repeats:YES];
} 

- (void) doActions {
theLabel.hidden != theLabel.hidden;
}
- (void)startTimer {
    pauseTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(doActions) userInfo:nil repeats:YES];
} 

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