大家好我使用进度条指示我的应用程序中xml解析的状态。但它没有显示逐步更新,而是在整个xml解析结束后加载进度条,直到那时它是卡住了,没有任何进展。如何解决这个问题。任何人都可以帮助我。 我使用下面的代码
IBOutlet UIProgressView * threadProgressView;
threadProgressView.progress = 0.0;
[self performSelectorOnMainThread:@selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO];
- (void)makeMyProgressBarMoving
{
float actual = [threadProgressView progress];
if (actual < 1) {
threadProgressView.progress = actual + ((float)1/(float)10);
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(makeMyProgressBarMoving) userInfo:nil repeats:NO];
}
else{
/* something else */
}
}
答案 0 :(得分:1)
每隔1/10秒在主线程上创建一个计时器。让它击中你的“makeMyProgressBarMoving”功能。
myTimer = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(makeMyProgressBarMoving) userInfo:nil repeats:YES];
然后编辑您的函数,如下所示:
- (void)makeMyProgressBarMoving {
float actual = [threadProgressView progress];
if (actual < 1) {
threadProgressView.progress = actual + ((float)1/(float)10);
return;
}
//if you got here that means the progress view is greater than 1 so now you can kill your timer that you had running every 1/10th of a second.
[myTimer invalidate];
}