使用Multipeer Connectivity跟踪NSProgress中的更改

Ale*_*dro 2 ios multipeer-connectivity

我使用多重连接在蓝牙上发送文件.进度存储在名为Progress的变量中:

NSProgress* progress;
Run Code Online (Sandbox Code Playgroud)

并以这种方式访问​​它:

progress.fractionCompleted
Run Code Online (Sandbox Code Playgroud)

当数字发生变化时,如何调用方法来更新我的UIprogressBar?

有一种方法:

-(void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress

{
NSLog(@"RECEIVING... %@ from peer: %@", progress, peerID);

}
Run Code Online (Sandbox Code Playgroud)

但它只被召唤一次......

Zee*_*Vax 5

你可以在fractionCompleted这样的东西上使用KVO

    [_progress addObserver:self 
        forKeyPath:@"fractionCompleted" 
           options:NSKeyValueObservingOptionNew 
           context:NULL];
Run Code Online (Sandbox Code Playgroud)

然后覆盖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];
}
Run Code Online (Sandbox Code Playgroud)

您可以查看Matt博客了解更多详情 http://www.raywenderlich.com/49850/whats-new-in-objective-c-and-foundation-in-ios-7