我正在使用多点连接通过蓝牙发送文件。进度存储在名为 Progress 的变量中:
NSProgress* progress;
它的访问方式是这样的:
progress.fractionCompleted
当数字发生变化时,如何调用方法来更新我的 UIprogressBar?
有一个方法:
-(void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress
{
NSLog(@"RECEIVING... %@ from peer: %@", progress, peerID);
}
但它只被调用一次......
最佳答案
你可以在fractionCompleted上使用KVO,类似这样
[_progress addObserver:self
forKeyPath:@"fractionCompleted"
options:NSKeyValueObservingOptionNew
context:NULL];
然后覆盖observeValueForKeyPath
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if (object == _progress) {
// Handle new fractionCompleted value
return;
}
[super observeValueForKeyPath:keyPath
ofObject:object
change:change
context:context];
}
您可以查看 Matt 博客了解更多详情 http://www.raywenderlich.com/49850/whats-new-in-objective-c-and-foundation-in-ios-7
关于ios - 通过多点连接跟踪 NSProgress 的变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19189310/