首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我调用[定时器NSTimer ]或[定时器失效]时,定时器崩溃。

当我调用[定时器NSTimer ]或[定时器失效]时,定时器崩溃。
EN

Stack Overflow用户
提问于 2010-09-12 03:32:39
回答 6查看 8.5K关注 0票数 12

我的iPhone应用程序中有两个NSTimers。DecreaseTimer运行良好,但当我调用TimerCountSeconds isValid或timerCountSeconds isValid时,timerCountSeconds会崩溃。它们的用法如下:

代码语言:javascript
复制
-(id)initialize { //Gets called, when the app launches and when a UIButton is pressed
 if ([timerCountSeconds isValid]) {
  [timerCountSeconds invalidate];
 } 
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //Gets called, when you begin touching the screen
 //....
 if ([decreaseTimer isValid]) {
   [decreaseTimer invalidate];
  }
 timerCountSeconds = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(runTimer) userInfo:nil repeats:YES];
 //....
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {//Gets called, when you stop touching the screen(not if you press the UIButton for -(id)initialize)
 //...
 decreaseTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(decrease) userInfo:nil repeats:YES];
 //...
}
-(void)comept3 { //Gets calles when you rubbed the screen a bit
    if ([timerCountSeconds isValid]) {
    [timerCountSeconds invalidate];
    }
}

我做错什么了?你能帮帮我吗?

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2010-09-12 04:10:29

您应该在对NSTimer对象执行invalidate之后将其设置为nil,因为invalidate方法调用也会执行release (根据苹果文档)。如果不这样做,在它上面调用像isValid这样的方法可能会导致崩溃。

票数 25
EN

Stack Overflow用户

发布于 2010-09-12 04:08:16

存储在该变量中的计时器很可能已经被释放。如果你想让它存在很长一段时间,你需要保留它。

票数 3
EN

Stack Overflow用户

发布于 2012-10-06 20:02:33

代码语言:javascript
复制
 [objTimer retain];

那么它就不会在任何时候崩溃。在初始化计时器之后使用这个,这样它就会工作得很好...

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3692359

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档