2013-02-24 46 views
5

我在编写代码以访问用户Twitter帐户,但在处理设备上没有帐户的情况时遇到问题。我想要做的是显示一条警告,告诉用户为了通过Twitter进行身份验证,他们首先需要在其设置中创建该帐户。UIAlertView无法在完成处理程序块中工作

这里是我的代码:

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线程发生,约25个电话后,深[警报显示]被调用,在过程称为ucol_getVersion。调试器指出EXC_BAD_ACCESS(代码= 2,地址= 0xcc)。

我已经在堆栈溢出和大型网络上搜索有关UIAlertViews不工作在块中的解决方案,以及显示警报(我为代表尝试了不同值)的一般问题以及常见问题调用requestAccessToAccountsWithType。

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

任何帮助表示赞赏。

回答

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秒。

+0

绝对正确..布拉沃... +1 – 2013-09-26 13:01:35

+0

但如果我想调用此alertview的委托方法。 ?? – 2015-09-25 05:10:24