如果我需要显示实时数据,如何让 UIViewController 即时更新自身?例如,我想在视图上显示时钟。第二部分每秒递增 1。我应该调用哪个方法让 UIViewController 有一个后台进程来自动更改内容而不显式刷新?
问问题
318 次
2 回答
0
你不需要更新整个事情,你只需要更新时钟。因此,例如,如果您使用数字时钟,您可以这样做:
- (void)clockTimerStart{
//call this in your viewDidLoad method
//start the clock
NSTimer *clockTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateClock) userInfo:nil repeats:YES];
}
然后,对于你的updateClock
方法
- (void)updateClock{
//this will be called every second while the clockTimer method is running
//you could increment the time here. For example, if your incrementing
//variable was named "increment"
increment++;
}
然后,当你想停止 yoru 时钟时,你可以使用clockTimer = nil
于 2013-11-15T04:19:51.127 回答
0
UIViewController
如果您只想更新时钟对象,则不需要更新整体。
每秒更新一次时钟的一种方法是使用NSTimer
. 文档在这里。
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateClock:) userInfo:nil repeats:YES];
于 2013-11-14T19:21:05.167 回答