2011-05-25 96 views
0

我已经搜索了这个问题,唯一的提示是performSelectorOnMainThread解决方案。我这样做了,但在for循环运行时视图不会更新。循环后,进度值正确显示 - 但这不是“进度”视图的要点=)在for循环中更新UIProgressView

for循环位于控制器内。我调用一个方法来设置进度值:

CGFloat pr = 0.5; // this value is calculated 
[self performSelectorOnMainThread:@selector(loadingProgress:) withObject:[NSNumber numberWithFloat:pr] waitUntilDone:NO]; 

这是加载进度方法

-(void)loadingProgress:(NSNumber *)nProgress{ 
    NSLog(@"Set Progress to: %@", nProgress); 
    [[self progress] setProgress:[nProgress floatValue]]; 
} 

我也试图通过使用这把法for循环到后台线程方法的顶部:

if([NSThread isMainThread]) { 
     [self performSelectorInBackground:@selector(importData) withObject:nil]; 
     return; 
    } 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

也许有人可以帮助我这个。

+0

这是正确的,让在另一个线程运行循环,你似乎更新进度视图的方式也似乎是正确的。两个问题:首先,你的NSLog是否显示设置了正确的值?其次,你确定没有其他东西阻塞主线程吗? – DarkDust 2011-05-25 09:53:43

+0

在for循环完成后,NSLog消息被放出。值是正确的 - 但太迟了;) - 我不确定,其他东西是阻止主线程,但我可以说,我什么也没做,只是显示视图:) – cem 2011-05-25 09:57:59

+0

滑稽:日志消息显示WHILE for循环正在运行,UIProgressView的更新是在for循环运行之后进行的。 – cem 2011-05-25 10:08:42

回答

0
CGFloat pr = 0.5; // this value is calculated 
[self performSelectorInBackground:@selector(loadingProgress:) withObject:[NSNumber numberWithFloat:pr]]; 

试试这个设置进度值

+0

UI更新必须在主线程上完成。如果你想在后台调用方法“loadingProgress”,你必须将setProgress嵌入到主线程中执行的块中,这是毫无意义的。 – foOg 2013-06-26 14:05:30