NSTimer更新标签 [英] NSTimer to update label

查看:50
本文介绍了NSTimer更新标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTimerDisplay) userInfo:nil repeats:YES];
[runLoop addTimer:self.timer forMode:NSRunLoopCommonModes];

此代码段是从我的viewDidLoad方法复制的,因此是从主线程运行的.它所做的就是调用一个方法来更新标签.

This code-snippet is copied from my viewDidLoad method, so it is runned from the main-thread. All it do is to call a method to update a label.

我以为我需要一个自己的线程来执行此操作,但是在SO获得帮助后,我发现我没有.

I thought I need to have a own thread for doing this, but after getting help on this at SO I figured out that I did not.

但是,我不理解 NSRunLoopCommonModes .为什么行得通?

However, I do not understand the NSRunLoopCommonModes. Why does it work?

并且计时器更新标签,该标签是与表格视图在同一屏幕上的数字计数器",因此即使用户按住屏幕也无法停止计时器.

AND the timer updates the label which is a "digital counter" which is on the same screen as a tableview so it CAN'T stop the timer even if the user holds the screen.

谢谢.

推荐答案

NSRunLoop可以在不同的输入模式下运行.该模式定义当前运行循环处理的事件.
例如:如果当前运行循环处于事件跟踪模式,则它仅处理模式事件循环.(例如,在Mac上拖动NSScrollBar或NSSlider)

A NSRunLoop can run in different input modes. The mode defines which events are handled by the current runloop.
e.g.: If the current runloop is in event tracking mode, it only handles modal event loops. (e.g. dragging a NSScrollBar or a NSSlider on the Mac)

如果仅为NSDefaultRunLoopMode添加NSTimer,则在引起模态事件循环的情况下不会触发.( Apple的文档)

If you add your NSTimer only for NSDefaultRunLoopMode it won't fire if something is causing a modal event loop. (Details in Apple's documentation)

NSRunLoopCommonModes是多种模式的别名",因此您不必这样做:

NSRunLoopCommonModes is an "alias" for multiple modes so that you don't have to do:

[[NSRunLoop currentRunLoop] addTimer:mRenderDurationTimer forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] addTimer:mRenderDurationTimer forMode:NSModalPanelRunLoopMode];
[[NSRunLoop currentRunLoop] addTimer:mRenderDurationTimer forMode:NSEventTrackingRunLoopMode];

这篇关于NSTimer更新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆