简体   繁体   English

从NSProgress更新UIProgressView

[英]Update UIProgressView from NSProgress

I have this observer and I am trying to update my UIProgressView in this way: 我有这个观察者,我试图以这种方式更新我的UIProgressView:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSProgress *progress = object;
NSLog(@"PROG: %f", progress.fractionCompleted);
[progressBar setProgress:_progressReceive.fractionCompleted animated:YES];

// Check which KVO key change has fired
if ([keyPath isEqualToString:kProgressCancelledKeyPath]) {
    // Notify the delegate that the progress was cancelled
    [progressBar removeFromSuperview];
    NSLog(@"CANCEL");

}
else if ([keyPath isEqualToString:kProgressCompletedUnitCountKeyPath]) {
    // Notify the delegate of our progress change

    if (progress.completedUnitCount == progress.totalUnitCount) {
        // Progress completed, notify delegate
        NSLog(@"COMPLETE");
        [progressBar removeFromSuperview];
    }
}
}

The NSLog is displaying data: NSLog正在显示数据:

...
2013-10-29 19:55:26.831 Cleverly[2816:6103] PROG: 0.243300
2013-10-29 19:55:26.835 Cleverly[2816:6103] PROG: 0.243340
2013-10-29 19:55:26.838 Cleverly[2816:6103] PROG: 0.243430
...

But the progressBar is not updating. 但progressBar没有更新。 I notice that if I use an NSTimer instead it does update: 我注意到如果我使用NSTimer而不是更新:

-(void)progressSend:(NSTimer*)timer{
NSLog(@"Fraction Complete: %@", [NSNumber numberWithDouble:progressSend.fractionCompleted]);
[progressBar setProgress:progressSend.fractionCompleted animated:YES];
}

Why is this?? 为什么是这样??

  1. Make sure that the values are right 确保值正确
  2. Make sure that your KVO is on the main thread. 确保您的KVO在主线程上。
  3. If not, dispatch_async the block of code that is changing UI on the main queue. 如果没有,则dispatch_async在主队列上更改UI的代码块。

try to perform on main thread: 尝试在主线程上执行:

[self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSArray arrayWithObjects:progressBar, progress.fractionCompleted, nil] waitUntilDone:NO];

Method: 方法:

-(void) updateProgress:(NSArray*)array
{
    UIProgressView *progressBar = [array objectAtIndex:0];
    NSNumber *number = [array objectAtIndex:1];
    [progressBar setProgress:number.floatValue animated:YES];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM