首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用KVO跟踪NSProgress

使用KVO跟踪NSProgress
EN

Stack Overflow用户
提问于 2013-10-27 21:01:59
回答 1查看 1.9K关注 0票数 1

我正在使用KVO跟踪正在接收的文件的进度:

代码语言:javascript
复制
-(void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress

{
NSLog(@"RECEIVING... %@ from peer: %@", progress, peerID);
dispatch_sync(dispatch_get_main_queue(), ^{
    [progress addObserver:self
                forKeyPath:@"fractionCompleted"
                   options:NSKeyValueObservingOptionNew
                   context:NULL];
});}

代码语言:javascript
复制
- (void)observeValueForKeyPath:(NSString *)keyPath
                  ofObject:(id)object
                    change:(NSDictionary *)change
                   context:(void *)context
{
if (object == progress) {
    // Handle new fractionCompleted value
[progressBar setProgress:progress.fractionCompleted animated:YES];
    NSLog(@"Fraction Complete: %@", [NSNumber numberWithDouble:progress.fractionCompleted]);
    return;
}

[super observeValueForKeyPath:keyPath
                     ofObject:object
                       change:change
                      context:context];
}

我想用这个来更新一个UIprogressView.但是代码在这一行上崩溃了,我不明白为什么:

代码语言:javascript
复制
[super observeValueForKeyPath:keyPath
                     ofObject:object
                       change:change
                      context:context];

编辑:

这是错误

代码语言:javascript
复制
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason:     '<ViewController1: 0x15b57000>: An -observeValueForKeyPath:ofObject:change:context: message was     received but not handled.

编辑:

如果删除超级观察者observeValueForKeyPath:keyPath.,应用程序不会崩溃,但是NSLog(@“分数完成:%@总是报告值"0”)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-27 23:23:56

您的超类有observeValueForKeyPath:ofObject:change:context:实现吗?您可以对此进行一次if检查:

代码语言:javascript
复制
if ([super respondsToSelector:@selector(observeValueForKeyPath:ofObject:change:context:){
    NSLog(@"Yeah, I'm here matey!");
} else {
    NSLog(@"Uh oh said the selector ghost");
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19623509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档