UIAlertView在完成处理程序块中不起作用

时间:2013-02-24 21:13:20

标签: ios objective-c

我正在编写代码以访问用户的Twitter帐户,但在处理设备上没有帐户的情况时遇到问题。我想做的是显示警告告诉用户,为了通过Twitter进行身份验证,他们首先需要在他们的设置中创建帐户。

这是我的代码:

self.accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountTypeTwitter = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

[self.accountStore requestAccessToAccountsWithType:accountTypeTwitter options:nil completion:^(BOOL granted, NSError *error) {
    if(granted) {
        //Doesn't matter for the present case
    } else if (error){

        [SVProgressHUD dismiss]; //not directly relevant to the present question, but it also doesn't work within the block, so I'm leaving it as a clue

        switch ([error code]){
            case (6):

                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"No Twitter Account Detected" message:@"Please go into your device's settings menu to add your Twitter account." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alert show]; //triggers an error
                break;

        }
    } 
}];

我不是Xcode调试器的专家,但显然错误发生在ACAccountStoreReply线程中,在调用[alert show]之后大约25次调用,在一个名为ucol_getVersion的进程中。调试器指出EXC_BAD_ACCESS(代码= 2,地址= 0xcc)。

我在Stack Overflow和整个互联网上搜索了关于UIAlertViews无法在块中工作的解决方案,以及显示警报的一般问题(我为代理尝试了不同的值),以及调用的一般问题requestAccessToAccountsWithType。

我还尝试通过阅读各种在线资源以及Objective-C编程的相关页面,第四次补充来了解我对块的理解。

感谢任何帮助。

1 个答案:

答案 0 :(得分:21)

您应该始终确保在主线程上完成任何UI交互。将您的UIAlertView来电换入dispatch_async

dispatch_async(dispatch_get_main_queue(), ^{
   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Twitter Account Detected" message:@"Please go into your device's settings menu to add your Twitter account." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
   [alert show];
});

在通话结束时你还有两个nil

相关问题