簡體   English   中英

UIAlertView在完成處理程序塊中不起作用

[英]UIAlertView doesn't work in completion handler block

我正在編寫代碼以訪問用戶的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編程的相關頁面,第四次補充來了解我對塊的理解。

任何幫助表示贊賞。

您應始終確保在主線程上完成任何UI交互。 dispatch_async包裝您的UIAlertView調用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM