2012-09-17 21 views
43

我想弄清楚爲什麼即時通訊崩潰在我的應用程序。在UIAlertView在iOS6 EXC_BAD_ACCESS代碼2

它工作在Xcode 4.4完美的罰款與ios5.1的模擬器中運行,但是當我切換到的Xcode 4.5和iOS6的我得到一個EXC_BAD_ACCESS碼2。這裏是我的代碼:

- (void) myMethod 
{ 
    UIAlertView *alertview = [[[UIAlertView alloc]initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil] autorelease]; 
    alertview.tag = 1 
    [alertview show]; 
} 

這是給我一個EXC_BAD_ACCESS代碼2上的[UIAlertView show]

有什麼想法嗎?

謝謝!

回答

126

我明白了。 我有同樣的問題,在我的情況下,似乎該方法是從後臺拋出(現在在ios7中,在ios6中,UIAlertView被自動置於主線程中,因爲@nodepond說 - 謝謝! - )..

儘量保證從主線顯示的方法:

[alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES]; 

祝你好運!

+4

好的調試技能!肯定工作 –

+3

我只想補充說,這個崩潰也發生在應用程序啓動時,如果用戶回家並返回,警報視圖顯示將導致崩潰。執行SelectorOnMainThread ...將按照Eva的建議修復它。 – Tommy

+0

太棒了!這幫助我解決了我的問題! – Patrik

0

它發生在我身上,即使在2014年。 問題是想使用已經發布的對象。

我做了什麼錯:

//class B with UIAletViewDelegate 

-(void) showAlert{ 
UIAlertView * alert = [[UIAlertView alloc] initWithTitle bla bla...]; 
[alert show]; 
} 


//class A 
viewDidLoad{ 
MyClassB *B = [[B alloc] init]; 
[B showAlert]; 
} 

什麼是正確的做法:

//Class A 
@implementation A{ 
    ClassB *B; 
} 

viewDidLoad{ 
    B = [[B alloc] init]; 
    [B showAlert]; 
} 
相關問題