Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 拖动UITableView时NSTimer不工作_Iphone_Objective C_Nstimer - Fatal编程技术网

Iphone 拖动UITableView时NSTimer不工作

Iphone 拖动UITableView时NSTimer不工作,iphone,objective-c,nstimer,Iphone,Objective C,Nstimer,我有一个带倒计时的应用程序。我用一个标签制作了它,该标签通过从计时器调用的函数进行更新,如下所示: ... int timeCount = 300; // Time in seconds ... NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(actualizarTiempo:) userInfo:nil repeats:YES]; ... - (void)ac

我有一个带倒计时的应用程序。我用一个标签制作了它,该标签通过从计时器调用的函数进行更新,如下所示:

...
int timeCount = 300; // Time in seconds
...
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(actualizarTiempo:) userInfo:nil repeats:YES];
...
- (void)actualizaTiempo:(NSTimer *)timer {
    timeCount -= 1;
    if (timeCount <= 0) {
        [timer invalidate];
    } else {
        [labelTime setText:[self formatTime:timeCount]];
    }
}
。。。
int timeCount=300;//以秒为单位的时间
...
NSTimer*myTimer=[NSTimer scheduledTimerWithTimeInterval:1目标:自选择器:@selector(RealizarTiempo:)用户信息:无重复:是];
...
-(无效)实施时间:(NSTimer*)计时器{
时间计数-=1;

如果(timeCount这是正常行为。
scheduledTimerWithTimeInterval:
对当前运行循环进行调度,因此与UITableView的任何交互都将阻止调用
RealizarTiempo:
方法。使用NSThread方法
detachNewThreadSelector:toTarget:withObject
sleepForTimeInterval
要在单独的线程上执行
RealizarTiempo:

使用
scheduledTimerWithTimeInterval:
,正如j.tom.schroeder所说,您的计时器会自动在主运行循环上为默认模式进行调度。这将防止您的计时器在运行循环处于非默认模式时触发,例如,在点击或滑动时。

不过,解决方案不是使用线程,而是为所有常见模式安排计时器:

NSTimer *timer = [NSTimer timerWithTimeInterval:1
                                         target:self
                                       selector:@selector(actualizarTiempo:)
                                       userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

根据您希望允许的事件类型而不必停止计时器,您也可以考虑<代码> UITrackingRunLoopMode <代码>。有关运行循环模式的更多信息,请参阅

< P> >快速版本:

Swift 2

var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "removeFromSuperview", userInfo: nil, repeats: false)
NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
Swift 3、4、5

var timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(removeFromSuperview), userInfo: nil, repeats: false)
RunLoop.main.add(timer, forMode: RunLoop.Mode.common)
Swift版本4+:

var timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.TimerController), userInfo: nil, repeats: true)
RunLoop.main.add(timer, forMode: RunLoopMode.commonModes)

如果您想重复,例如作为计数器,则设置repeat=true else=false

非常感谢您的帮助,它与此解决方案配合使用。我将仔细查看循环模式,以获得更适合我的事件的模式不要忘了
[计时器失效]
视图上的
将消失
或任何其他方法,或者您将一次又一次地启动计时器。实际上,问题与计时器与当前运行循环相关。将NSRunLoop与不同的循环模式一起使用可修复此问题。感谢SWIFT 3+版本:RunLoop.main.add(timer,forMode:RunLoopMode.commonModes)