2

我正在使用多点连接,这是其中一种方法:

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

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

UIProgressView *progressBar = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
progressBar.frame = CGRectMake(0, 200, 100, 20);
progressBar.progress = 0.5;
UIButton* btn = [BluetoothDeviceDictionary objectForKey:peerID];
[self.view addSubview:progressBar];

UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"Alert View Title" message:@"Alert View Text" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alertView show];
}

由于某些非常奇怪的原因,当调用此方法时,我知道它是由于 NSLog 的响应而被调用的,其余代码没有执行。在 NSLog 出现后 20 秒(或多或少)出现警报,并且永远不会出现进度视图。我不明白为什么。对于多点连接框架中的大多数方法,都会发生这种情况。这怎么可能?

编辑:实际上出现了进度视图,但是在调用该方法之后很多

4

1 回答 1

5

会话委托的方法可能在后台线程上被调用。UIKit 调用应该只在主线程上进行,因此您可能需要将与 UIKit 交互的代码移动到另一个方法,如下所示:

- (void) updateUI {
    UIProgressView *progressBar = [[UIProgressView alloc];
    initWithProgressViewStyle:UIProgressViewStyleBar];
    progressBar.frame = CGRectMake(0, 200, 100, 20);
    progressBar.progress = 0.5;
    UIButton* btn = [BluetoothDeviceDictionary objectForKey:peerID];
    [self.view addSubview:progressBar];

    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"Alert View Title" message:@"Alert View Text" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alertView show];
}

然后使用以下命令调用它:

-(void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress
{
    NSLog(@"RECEIVING... %@ from peer: %@", progress, peerID);
    [self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:YES];
}
于 2013-10-02T20:47:28.093 回答