简体   繁体   English

目标C:计时器在再次打开时使应用程序崩溃

[英]Objective C: Timer crashes app when turned on a second time

I have my NSTimer declared in the header file. 我在头文件中声明了我的NSTimer。 When the play button is tapped, it turns on the timer. 轻按play按钮后,它将打开计时器。 Next tap disables timer. 下次点击禁用计时器。 A third tap crashes the app with no error message. 第三次敲击该应用程序,没有错误消息。 Why would this be happening? 为什么会这样呢?

-(IBAction) play {

if ([col1 isValid]) {
    [col1 invalidate];
} else {
    col1 = [NSTimer scheduledTimerWithTimeInterval:.8 target:self selector:@selector(playCol1) userInfo:nil repeats:YES];
}

invalidate will cause the object to be released. invalidate将导致对象被释放。 So, you are attempting to call isValid on a bad reference. 因此,您试图在错误的引用上调用isValid nil out the col1 object after you invalidate it, and you should be fine. 使col1对象无效后,将其取消,这应该没问题。

-(IBAction) play {

    if ([col1 isValid]) {
        [col1 invalidate];
        col1 = nil;
    } else {
        col1 = [NSTimer scheduledTimerWithTimeInterval:.8 target:self selector:@selector(playCol1) userInfo:nil repeats:YES];
}

Crash is because it is trying to invalidate an already invalidated timer col1 . 崩溃是因为它试图invalidate已经无效的计时器col1无效。

Did you try [col1 isValid]==YES ? 您是否尝试过[col1 isValid]==YES Try out that and if that doesn't work then try using an custom integer or BOOL type flag. 尝试一下,如果不起作用,请尝试使用自定义integerBOOL类型标志。

I had a similar problem with the isValid Method and made a workaround for it by using flag of BOOL type 我对isValid方法有类似的问题,并使用BOOL类型的标志对其进行了解决

Hope this helps you. 希望这对您有所帮助。

SAMPLE OF USAGE OF CUSTOM FLAGS: 定制标志的使用示例:

 BOOL invalidateTimer = NO; 
-(IBAction) play 
{
     if (invalidateTimer == YES) 
     {
                 [col1 invalidate];
     }
     else if (invalidateTimer == NO)
     {
           col1 = [NSTimer scheduledTimerWithTimeInterval:.8 target:self selector:@selector(playCol1) userInfo:nil repeats:YES];
           invalidateTimer = YES;
     }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 目标C短信选项崩溃整个应用程序? - objective C SMS option crashes the entire App? 目标C整数第一次是0,但是第二次改变 - Objective C Integer is 0 first time, but it changes second time 我的Iphone应用在侧面打开(横向模式)时崩溃(访问错误) - My Iphone App crashes (Bad access) when turned on it's side (landscape mode) 未知原因导致CFStringAppend期间Objective-C iOS应用程序崩溃 - Objective-C iOS app crashes during CFStringAppend for unknown reason 目标C:将char参数传递给函数会使应用程序崩溃 - objective c: passing a char argument to function crashes the app 如何在目标c中使用timer调用函数时传递参数 - How to pass arguments when calling function with timer in objective c GPUImage。 尝试再次保存照片时,应用程序崩溃 - GPUImage. Application crashes when trying to save photo second time 从UITableView移至第二视图时,应用程序崩溃 - App crashes when moving from UITableView to second view 目标c:如何在应用处于后台/根据时间间隔自动终止时执行功能 - Objective c: how to execute a function when app is in the background/killed automatically based on time Intervals 具有时区的日期时间对象在目标C应用程序开发中不起作用 - Date time object with time zone not working in objective C app development
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM