我可以从互联网上下载ZIP文件。后期处理是在connectionDidFinishLoading中完成的,除了没有更新UIView元素之外,一切正常。例如,我设置了statusUpdate.text =@“解压缩文件”,但是直到connectionDidFinishLoading完成后,这个更改才会出现。类似地,在此方法结束之前,不会更新UIProgressView和UIActivityIndicatorView对象。
有没有办法从这个方法中强制更新UIView?我尝试设置self.view setNeedsDisplay,但不起作用。它似乎是在主线程中运行的。这里的所有其他命令都工作得很好-唯一的问题是更新UI。
谢谢!
更新:以下是未更新UIVIEW的代码:
-(void)viewWillAppear:(BOOL)animated {
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(processUpdate:) userInfo:nil repeats:YES];
downloadComplete = NO;
statusText.text = @"";
}
-(void)processUpdate:(NSTimer *)theTimer {
if (! downloadComplete) {
return;
}
[timer invalidate];
statusText.text = @"Processing update file.";
progress.progress = 0.0;
totalFiles = [newFiles count];
for (id fileName in newFiles) {
count++;
progress.progress = (float)count / (float)totalFiles;
// ... process code goes here ...
}
}在processUpdate的末尾,我设置了downloadComplete = YES。这个构建和运行没有错误,并按预期工作,除非在processUpdate完成之前,UIVIEW中没有任何更新,然后所有内容都会立即更新。
谢谢你到目前为止的帮助!
发布于 2012-12-10 02:09:42
问题是UI没有在for()循环中更新。查看the answer in this thread获取简单的解决方案!
发布于 2010-01-09 04:12:46
正如Niels所说,如果你想看到视图更新,你必须将控制返回给run循环。但是除非你真的需要,否则不要开始分离新的线程。我推荐这种方法:
- (void)connectionDidFinishLoading:(NSConnection *)connection {
statusUpdate.text = @"Uncompressing file";
[self performSelector:@selector(doUncompress) withObject:nil afterDelay:0];
}
- (void)doUncompress {
// Do work in 100 ms chunks
BOOL isFinished = NO;
NSDate *breakTime = [NSDate dateWithTimeIntervalSinceNow:100];
while (!isFinished && [breakTime timeIntervalSinceNow] > 0) {
// do some work
}
if (! isFinished) {
statusUpdate.text = // here you could update with % complete
// better yet, update a progress bar
[self performSelector:@selector(doUncompress) withObject:nil afterDelay:0];
} else {
statusUpdate.text = @"Done!";
// clean up
}
}基本的想法是,你可以在小块中工作。您可以从您的方法返回,以允许run循环定期执行。对performSelector:的调用将确保控制最终返回到您的对象。
请注意,这样做的风险在于,用户可能会按下按钮或以某种您可能意想不到的方式与UI交互。在工作时调用be应用程序的beginIgnoringInteractionEvents以忽略输入可能会有所帮助……除非你想表现得很好,并提供一个cancel按钮来设置你签入doUncompress方法的标志……
您也可以尝试自己运行run循环,偶尔调用[[NSRunLoop currentRunLoop] runUntilDate:...],但我从未在自己的代码中尝试过。
发布于 2010-01-03 18:36:11
当您在connectionDidFinishLoading中时,应用程序运行循环中不会发生任何其他事情。需要将控制传递回run循环,以便它可以编排UI更新。
只需将数据传输标记为完成,并将视图标记为更新即可。将下载数据的任何繁重处理推迟到它自己的线程。
应用程序将回调您的视图,让它们稍后在run循环中刷新其内容。在您自己的自定义视图上适当地实现drawRect。
https://stackoverflow.com/questions/1994495
复制相似问题