我有一个带有三个标签的标签栏应用程序。在我的第二个标签视图中,我有一个UIProgressView
。我的viewDidLoad
方法中有一个NSTimer,它调用一个更新progressView
progressTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(updateProgress) userInfo:nil repeats:YES];
到目前为止一切都很好。
我想知道如何在应用程序的整个生命周期中在后台运行计时器,以便无论用户在哪个视图中progressView
都会更新。如果你能给我看,那就太棒了使用代码段。
提前致谢。
答案 0 :(得分:1)
不建议从后台线程更新UI元素。此外,当用户不在该视图中时,不建议修改视图的UI元素。您正在使用宝贵的系统资源。
更好的方法是在视图变为活动状态后立即更新ProgressBar ...
更新:您可以看到here知道如何在后台线程中运行任务。您可以将NSTimer
设置为在您指定的选择器中启动。但同样,这种东西可能会导致一些奇怪的错误。最好避免......
答案 1 :(得分:0)
您可以在ProgressViewController中保留一个属性(甚至在app委托中),比如跟踪进度的float
。当您需要更新进度时,另一个ViewController可以更改属性的值。
视图显示后,从UIProgressView
更新用户界面(viewWillAppear:
)。
答案 2 :(得分:0)
你可以用这种方式在后台添加方法
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Add code here to do background processing
// increment the progress here and keep the property of that count in appdelegate
//
dispatch_async( dispatch_get_main_queue(), ^{
// Add code here to update the UI/send notifications based on the
// results of the background processing
});
});
在diffent视图的viewWillAppear你可以这样设置: [loadingProgressView setProgress:appDelegate.count];