UIViewController动态更新自己?

时间:2013-11-14 19:06:53

标签: ios cocoa-touch uiviewcontroller

如果我需要显示实时数据,如何让UIViewController动态更新?例如,我想在视图上显示时钟。第二部分每秒递增1。我应该调用哪种方法让UIViewController有一个后台进程自动更改内容而不显式刷新?

2 个答案:

答案 0 :(得分:0)

如果您只想更新时钟对象,则无需更新整个UIViewController

一次更新时钟的一种方法是使用NSTimer。文档here

[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateClock:) userInfo:nil repeats:YES];

答案 1 :(得分: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