2

我试图从一个回调块内推视图控制器内执行上mainthread UIKit的电话。我想要推送的视图控制器包含一个UIWebView,它抱怨我应该在主线程上调用它。我尝试使用NSInvocation无济于事。我如何在主线程上调用presentModalViewController:animated如何从一个块

[vc requestUserPermissionWithCallback:^(BOOL inGranted) 
     { 
      if (inGranted) 
      { 
       if (serviceType == PXServiceTypeTwitter) 
       { 
        //[ad.rootViewController presentModalViewController:vc animated: YES]; // crashes 
        NSInvocation *invocation = [NSInvocation invocationWithTarget:ad.rootViewController selector:@selector(presentModalViewController:animated:) retainArguments:YES, vc, YES]; 
        [invocation invokeOnMainThreadWaitUntilDone:NO]; // same result 
       } 
      } 
      else 
      { 
       [self.navigationController popToRootViewControllerAnimated:YES]; 
      } 
     }]; 

错误消息:

布尔_WebTryThreadLock(布尔),0x6b21090:尝试以获得比从主线程或web线程以外的线程的网络锁定。这可能是从辅助线程调用UIKit的结果。现在崩溃...

我使用的类别为NSInvocation+CWVariableArguments

回答

8

只需使用dispatch_get_main_queue()让主线程,然后分派到:

dispatch_async(dispatch_get_main_queue(), ^{ 
    // whatever code you want to run on the main thread 
}); 
+0

工作过,也很简单。 – Morrowless 2012-02-23 10:44:57

0

我也有类似的问题,使用

performSelectorOnMainThread:withObject:waitUntilDone: 

来解决它。希望有所帮助。

相关问题