iPhone - NSTimer 在火灾后不重复

发布于 2024-10-13 07:41:41 字数 605 浏览 3 评论 0原文

我正在创建并触发一个 NSTimer

ncTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                           target:self
                                         selector:@selector(handleTimer:)
                                         userInfo:nil
                                          repeats:YES];
[ncTimer fire];

并且

- (void)handleTimer:(NSTimer *)chkTimer {
    // do stuff
}

我正在保留我的计时器:

@property (nonatomic, retain) NSTimer *ncTimer;

出于某种原因,计时器不重复。它只会发射一次,并且不会再发射。

I am creating and firing a NSTimer with:

ncTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                           target:self
                                         selector:@selector(handleTimer:)
                                         userInfo:nil
                                          repeats:YES];
[ncTimer fire];

AND

- (void)handleTimer:(NSTimer *)chkTimer {
    // do stuff
}

I am retaining my timer with:

@property (nonatomic, retain) NSTimer *ncTimer;

For some reason the timer is not repeating. It is firing once only and than never again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

七色彩虹 2024-10-20 07:41:41

-fire: 方法手动触发一次。要启动并重复计时器,您必须使用 [[NSRunLoop currentRunLoop] addTimer: forMode:] 将其添加到运行循环中

The -fire: method manually fires it once. For a timer to be started and repeat, you have to add it to a runloop using [[NSRunLoop currentRunLoop] addTimer: forMode:]

☆獨立☆ 2024-10-20 07:41:41

明白了,

将计时器添加到 mainRunLoop 使其正常工作

Got it

Adding timer to mainRunLoop made it working ????????????

Here you go:

Objective C:

self.ncTimer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

Swift 2

var ncTimer = NSTimer(timeInterval: 2.0, target: self, selector: Selector("handleTimer"), userInfo: nil, repeats: true)
NSRunLoop.mainRunLoop().addTimer(ncTimer, forMode: NSDefaultRunLoopMode)

Swift 3, 4, 5

var ncTimer = Timer(timeInterval: 2.0, target: self, selector: #selector(self.handleTimer), userInfo: nil, repeats: true)
RunLoop.main.add(ncTimer, forMode: RunLoop.Mode.default)
悲凉≈ 2024-10-20 07:41:41

您不能只分配给作为属性放入标头中的计时器。这应该有效:

self.ncTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self selector:@selector(handleTimer:) userInfo:nil repeats: YES];

另外: fire 方法会在循环外触发计时器。如果计时器不重复,则其无效。在显示“fire”的行之后,添加以下内容:


BOOL timerState = [ncTimer isValid];
NSLog(@"Timer Validity is: %@", timerState?@"YES":@"NO");

You can't just assign to the timer that you have put as a property in your header. This should work:

self.ncTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self selector:@selector(handleTimer:) userInfo:nil repeats: YES];

Also: The fire method fires the timer, out of cycle. If the timer is non repeating it is invalidated. After the line that says fire, add this:


BOOL timerState = [ncTimer isValid];
NSLog(@"Timer Validity is: %@", timerState?@"YES":@"NO");
人海汹涌 2024-10-20 07:41:41

您还可以将代码复制到该块内,这会在主线程中插入计时器的创建。

因此,代码将保留:

dispatch_async(dispatch_get_main_queue(), ^{
  self.ncTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                 target:self selector:@selector(handleTimer:) userInfo:nil repeats: YES];
});

You can also copy your code inside this block, which inserts the creation of the Timer in the main thread.

The code will therefore remain:

dispatch_async(dispatch_get_main_queue(), ^{
  self.ncTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                 target:self selector:@selector(handleTimer:) userInfo:nil repeats: YES];
});
维持三分热 2024-10-20 07:41:41

我不知道为什么,但 Timer.scheduledTimer 方法不起作用,但 Timer.init 方法起作用。

self.timer = Timer.init(timeInterval: 10.0, repeats: true, block: { (timer) in
            print("\n--------------------TIMER FIRED--------------\n")
            self.checkForDownload()
        })
RunLoop.main.add(self.timer!, forMode: RunLoopMode.defaultRunLoopMode)

I don't know why but Timer.scheduledTimer method is not working but Timer.init method worked.

self.timer = Timer.init(timeInterval: 10.0, repeats: true, block: { (timer) in
            print("\n--------------------TIMER FIRED--------------\n")
            self.checkForDownload()
        })
RunLoop.main.add(self.timer!, forMode: RunLoopMode.defaultRunLoopMode)
も让我眼熟你 2024-10-20 07:41:41

分配给 ncTimer 不会启动属性 retain 功能。

假设声明位于成员对象内,您需要执行以下操作:

self.ncTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES]

Assigning to ncTimer as you have will not initiate the properties retain functionality.

Assuming the declaration is within the member object you will need to do:

self.ncTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES]
家住魔仙堡 2024-10-20 07:41:41

您可以在 Swift 中编写相同的内容,而无需在主循环中运行:

ncTimer = Timer.scheduledTimer(timeInterval: 1.0, 
                               target: self, 
                               selector: #selector(self.handleTimer), 
                               userInfo: nil, 
                               repeats: true)
ncTimer.fire()

You could write the same thing in Swift without running in main loops by:

ncTimer = Timer.scheduledTimer(timeInterval: 1.0, 
                               target: self, 
                               selector: #selector(self.handleTimer), 
                               userInfo: nil, 
                               repeats: true)
ncTimer.fire()
梦过后 2024-10-20 07:41:41

带闭包的 Swift 定时器:

    let timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
         // Do whatever
    }
    timer.fire()

Swift timer with closure:

    let timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
         // Do whatever
    }
    timer.fire()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文