ios NSTimer timerWithTimeInterval:不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11058571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:48:31  来源:igfitidea点击:

NSTimer timerWithTimeInterval: not working

iostimernstimer

提问by Midhun MP

I've created a test application with timer before implementing it in my project. It was the first time I'm using timer. But the issue is when I implemented timer using [NSTimer timerWithTimeInterval: target: selector: userInfo: repeats: ];, it is not working. Here is my code, Interface:

在我的项目中实现它之前,我已经创建了一个带有计时器的测试应用程序。这是我第一次使用计时器。但问题是当我使用 实现计时器时[NSTimer timerWithTimeInterval: target: selector: userInfo: repeats: ];,它不起作用。这是我的代码,接口:

@interface uialertViewController : UIViewController
{

    NSTimer *timer;
}

-(void)displayAlert;
-(void)hideandview;
@end

Implementation:

执行:

@implementation uialertViewController
- (void)viewDidLoad {

    [self displayAlert];
    [super viewDidLoad];
}


-(void)displayAlert{

    timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(hideandview) userInfo:nil repeats:NO];

    alert = [[UIAlertView alloc] initWithTitle:@"testing" message:@"hi hi hi" delegate:nil cancelButtonTitle:@"continue" otherButtonTitles:nil];
    [alert show];




    [alert release];
    alert = nil;

}

-(void)hideandview{

    NSLog(@"triggered");

    [alert dismissWithClickedButtonIndex:0 animated:YES];

    [alert release];

    [self displayAlert];
}

@end

Then I Changed[NSTimer timerWithTimeInterval: target: selector: userInfo: repeats: ];with[NSTimer scheduledTimerWithTimeInterval: target: selector:userInfo: repeats: ];, It is working. What was the issue with timerWithTimeInterval:? Am I mising anything in my first implementation ? Thanks in advance.

然后我改变[NSTimer timerWithTimeInterval: target: selector: userInfo: repeats: ];[NSTimer scheduledTimerWithTimeInterval: target: selector:userInfo: repeats: ];,它正在工作。出了什么问题timerWithTimeInterval:?我在第一次实现中遗漏了什么吗?提前致谢。

回答by Joseph Humfrey

scheduledTimerWithTimeInterval:invocation:repeats:and scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:create timers that get automatically added to an NSRunLoop, meaning that you don't have to add them yourself. Having them added to an NSRunLoopis what causes them to fire.

scheduledTimerWithTimeInterval:invocation:repeats:scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:创建自动添加到 的计时器NSRunLoop,这意味着您不必自己添加它们。将它们添加到 anNSRunLoop是导致它们着火的原因。

With timerWithTimeInterval:invocation:repeats:and timerWithTimeInterval:target:selector:userInfo:repeats:, you have to add the timer to a run loop manually, with code like this:

使用timerWithTimeInterval:invocation:repeats:and timerWithTimeInterval:target:selector:userInfo:repeats:,您必须手动将计时器添加到运行循环中,代码如下:

[[NSRunLoop mainRunLoop] addTimer:repeatingTimer forMode:NSDefaultRunLoopMode];

Other answers on here suggest that you need to call fireyourself. You don't - it will be called as soon as the timer has been put on a run loop.

此处的其他答案表明您需要给fire自己打电话。你没有 - 一旦计时器被置于运行循环中,它就会被调用。

回答by Vladimir Shutyuk

Also one may want to make sure to add timer on the main thread.

另外,您可能希望确保在主线程上添加计时器。

assert(Thread.isMainThread)
let timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(YourSelector), userInfo: nil, repeats: true)

回答by Joe Pagliaro

As a previous answer noted schedule on the main thread, but rather than using assert, put it on the main thread:

正如之前的回答指出主线程上的调度,但不是使用断言,而是将其放在主线程上:

@objc func update() {
    ...
}

DispatchQueue.main.async {
            self.timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)
        }

And if async is not desired, try this:

如果不需要异步,请尝试以下操作:

let schedule = {
            self.timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)
        }

        if Thread.isMainThread {
            schedule()
        }
        else {
            DispatchQueue.main.sync {
                schedule()
            }
        }

回答by Chance Hudson

The difference between the two is that the timerWithTimeIntervalmethod returns a NSTimerobject that has not yet been fired. To fire the timer you have to use [timer fire];On the other hand the scheduledTimerWithTimeIntervalreturns an NSTimerthat has already been fired.

两者的区别在于该timerWithTimeInterval方法返回一个NSTimer尚未被触发的对象。要触发计时器,您必须使用[timer fire];另一方面,scheduledTimerWithTimeInterval返回一个NSTimer已经被触发的。

So, in your first implementation you were just missing [timer fire];

所以,在你的第一个实现中,你只是缺少 [timer fire];