2013-02-24 37 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

相關問題