2013-11-25 112 views
0

我有一個UIAlertView塊,我想點擊OK後運行一些代碼。UIAlertView在塊內部崩潰

- (IBAction)connectToAccount:(UIButton *)sender { 


     void (^block) (FTjsonRecords *obj, NSError *error) = ^(FTjsonRecords *obj, NSError *error) { 


       [self updateInterfaceWithReachability:self.hostReachability]; 

        UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Success!" 
                   message:@"Online sync is now enabled." 
                   delegate:self 
                 cancelButtonTitle:@"OK" 
                 otherButtonTitles:nil]; 
        [av show]; 


     }; 

     [sync checkFirstLogin:email viaPassword:password viaCompletion:block]; 
    } 

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
    [[self navigationController] popToRootViewControllerAnimated:NO]; 
    NSNotification *note = [NSNotification notificationWithName:IOS_SWITCH_TAB object:nil]; 
    [[NSNotificationCenter defaultCenter] postNotification:note]; 
} 

當我點擊確定,應用程序崩潰,沒有任何例外跟進。 我不知道如何繼續。建議會很好。由於

UPDATE:

我已經設置[av show];一個破發點的權利和螺紋看起來是這樣的:

enter image description here

+3

該塊是否在主隊列/線程上執行? –

+0

@斯科特我相信它是。請參閱最新的問題。謝謝 – Houman

+0

完成塊是否通過複製正確地移動到堆中? – Sulthan

回答

1

因爲所有的UI代碼必須在主線程中運行,您需要將您的警報代碼附在dispatch_async區塊中,如下所示:

dispatch_async(dispatch_get_main_queue(), ^{ 
    UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Success!" 
               message:@"Online sync is now enabled." 
               delegate:self 
             cancelButtonTitle:@"OK" 
    [av show]; 
}); 
+0

感謝您的提示。我只是試圖沒有任何成功。這是和以前一樣的問題。當我點擊OK時,它崩潰了。 – Houman

+0

因此它必須在'alertView:didDismissWithButtonIndex:'中的某處崩潰。你有沒有檢查過你的'note'對象是不是零? – Macondo2Seattle

+0

好吧,崩潰顯然是在'FTLoginViewController connectToAccount:''中。我不認爲警報視圖在這裏是一個問題。你可以發佈'connectToAccount'的代碼嗎? – Macondo2Seattle

1

我終於找到了問題。因爲它被認爲是一個線程問題,我不能先發現它。

在行:

[self updateInterfaceWithReachability:self.hostReachability]; 

我正在做的一個異步線程一些工作,並在那年底,我叫

[[self navigationController] popToRootViewControllerAnimated:YES]; 

這是當我展示彈出的發生兩人相撞。我從updateInterfaceWithReachability中刪除了popToRootViewControllerAnimated,並將其放入UIAlertView的委託中,現在它按預期工作。

我希望這可以幫助其他人,以類似的情況。

0

另外值得考慮的是UIAlertView的內存管理。如果在可見狀態下重新分配UIAlertView,它也會導致崩潰。