【发布时间】:2011-04-11 04:17:19
【问题描述】:
我的 iPhone 应用中有两个 NSTimer。 DecreaseTimer 工作正常,但是当我调用 [timerCountSeconds isValid] 或 [timerCountSeconds invalidate] 时 TimerCountSeconds 崩溃。它们是这样使用的:
-(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];
}
}
我做错了什么? 你能帮帮我吗?
【问题讨论】:
-
你初始化TimerCountSeconds和DecreaseTimer了吗?
-
我在头文件中声明了它们...我在“touchesEnded”和“touchesBegan”方法中初始化了它们
-
显示更多代码。此外,使用像
DecreaseTimer这样的名称作为实例变量通常是一个非常糟糕的主意。以大写字母开头的名称用于类和结构。考虑使用 Apple 提倡的一致风格。 -
好的,我改了变量名...我忘了输入“comept3”方法...这可能会帮助你帮助我;)
标签: iphone objective-c xcode crash nstimer