gpt4 book ai didi

ios - 更新在后台运行的标签的计时器

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:10:22 24 4
gpt4 key购买 nike

我使用更新标签的 NSTimer 编写了一个计时器。问题是,在同一个 View Controller 中,我有一个 uitableview,当我向下滚动它时,计时器不会更新它的值,因此用户可以“作弊”以停止计时器。

我认为使用 CGD 的串行队列可以很容易地解决这个问题,但我不知道该怎么做。

提前致谢

胡安

最佳答案

首先请记住,您不能在主线程以外的任何其他线程中执行 UI 更改。话虽如此,您需要 NSTimer 在主队列中触发,否则程序会在更改 UILabel 时崩溃。看看这个链接 http://bynomial.com/blog/?p=67还有这个http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSRunLoop_Class/Reference/Reference.html

据我所知,如果您在 NSRunLoopCommonModes 中安排计时器,它将忽略事件更新并按照您的需要触发计时器:

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

-(void) timerDidTick:(NSTimer*) theTimer{
[[self myLabel] setText:@"Timer ticked!"];
}

关于ios - 更新在后台运行的标签的计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16261620/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com