2

我正在尝试从回调块内部推送视图控制器。我要推送的视图控制器包含一个 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];
             }
         }];

错误信息:

bool _WebTryThreadLock(bool), 0x6b21090: 试图从除主线程或web线程之外的线程获取web lock。这可能是从辅助线程调用 UIKit 的结果。现在崩溃...

我正在使用类别NSInvocation+CWVariableArguments

4

2 回答 2

8

只需使用dispatch_get_main_queue()获取主线程,然后分派给它:

dispatch_async(dispatch_get_main_queue(), ^{
    // whatever code you want to run on the main thread
});
于 2012-02-23T10:43:43.773 回答
0

我有一个类似的问题并使用

performSelectorOnMainThread:withObject:waitUntilDone:

解决它。希望有帮助。

于 2013-02-26T19:23:21.923 回答