ios - 通过多点连接跟踪 NSProgress 的变化

标签 ios multipeer-connectivity

我正在使用多点连接通过蓝牙发送文件。进度存储在名为 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/

相关文章:

swift - 使用委托(delegate)方法在两个类之间发送数据

ios - 多点连接 IOS : didFinishReceivingResourceWithName Error Handling (Crash)

ios - Eureka SelectableSection 从 selectedRows 获取值

ios - 如何从雅虎天气预报 XML 文档中获取图像 url?

ios - 在多个 View Controller 中显示的通用或单个 uipickerview

ios - Base64解码

ios - iOS 中通过 wifi 的点对点网络

ios - 如何让我的应用程序在 13 英寸 iOS 平板电脑上正确缩放?

ios - 接受邀请多点连接

ios - Multipeer 连接范围和非 ios 设备