gpt4 book ai didi

iphone - 当我调用 [Timer isValid] 或 [Timer invalidate] 时,NSTimer 崩溃

转载 作者:太空狗 更新时间:2023-10-30 03:25:07 27 4
gpt4 key购买 nike

我的 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];
}
}

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

最佳答案

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

关于iphone - 当我调用 [Timer isValid] 或 [Timer invalidate] 时,NSTimer 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3692359/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com