ios9兼容版本的NSTimer scheduledTimerWithTimeInterval:repeat:block:?

时间:2021-08-19 20:37:51

I'm using the following code in iOS 10 but when I run it in iOS 9 it crashes. I don't think NSTimer scheduledTimerWithTimeInterval:repeats:block: supports iOS 9. How can I implement a timer that will work in iOS 8 - 10?

我在ios10中使用了下面的代码,但是当我在ios9中运行时它崩溃了。我不认为NSTimer scheduledTimerWithTimeInterval:repeat:block:支持iOS 9。如何实现在ios8 - 10中工作的计时器?

static NSTimer* timer = nil;

- (void)documentInteractionControllerWillBeginPreview:(UIDocumentInteractionController *)controller
{
    timer = [NSTimer scheduledTimerWithTimeInterval:0.1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        [[UIApplication sharedApplication] setStatusBarHidden:YES];
    }];
}

-(void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
{
    [timer invalidate];
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
}

2 个解决方案

#1


5  

You appear to be using a function introduced in iOS10. Further down the page you linked is a function that was in iOS 2.

您似乎正在使用iOS10中引入的函数。在你链接的页面下方有一个iOS 2中的函数。

https://developer.apple.com/documentation/foundation/nstimer/1408356-timerwithtimeinterval?language=objc

https://developer.apple.com/documentation/foundation/nstimer/1408356-timerwithtimeinterval?language=objc

Use that instead.

用这个代替。

#2


3  

This fixed it:

这固定:

static NSTimer* timer = nil;

- (void)documentInteractionControllerWillBeginPreview:(UIDocumentInteractionController *)controller
{
    timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(hideStatusBar) userInfo:nil repeats:YES];
}

-(void)hideStatusBar
{
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
}

-(void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
{
    [timer invalidate];
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
}

#1


5  

You appear to be using a function introduced in iOS10. Further down the page you linked is a function that was in iOS 2.

您似乎正在使用iOS10中引入的函数。在你链接的页面下方有一个iOS 2中的函数。

https://developer.apple.com/documentation/foundation/nstimer/1408356-timerwithtimeinterval?language=objc

https://developer.apple.com/documentation/foundation/nstimer/1408356-timerwithtimeinterval?language=objc

Use that instead.

用这个代替。

#2


3  

This fixed it:

这固定:

static NSTimer* timer = nil;

- (void)documentInteractionControllerWillBeginPreview:(UIDocumentInteractionController *)controller
{
    timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(hideStatusBar) userInfo:nil repeats:YES];
}

-(void)hideStatusBar
{
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
}

-(void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
{
    [timer invalidate];
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
}