2012-08-10 92 views
0

看起來像一個驚人的微不足道的差異。將otherButtonTitles的非nil值傳遞給UIAlerView會爆炸。iOS/Cocoa:UIAlertView投擲EXC_BAD_ACCESS

工作:

UIAlertView *alert = [[UIAlertView alloc] 
         initWithTitle:@"Login with your credentials" 
         message:nil 
         delegate:nil 
         cancelButtonTitle:@"Cancel" 
         otherButtonTitles:nil]; 

不工作:

UIAlertView *alert = [[UIAlertView alloc] 
         initWithTitle:@"Login with your credentials" 
         message:nil 
         delegate:nil 
         cancelButtonTitle:@"Cancel" 
         otherButtonTitles:@"OK"]; 

是怎麼回事?

回答

4
UIAlertView *alert = [[UIAlertView alloc] 
         initWithTitle:@"Login with your credentials" 
         message:nil 
         delegate:nil 
         cancelButtonTitle:@"Cancel" 
         otherButtonTitles:@"OK1",@"OK2",nil]; 

最後一個參數是一個多參數,應以零終止。

2

基本上,你應該有這個代替:

UIAlertView *alert = [[UIAlertView alloc] 
         initWithTitle:@"Login with your credentials" 
         message:nil 
         delegate:nil 
         cancelButtonTitle:@"Cancel" 
         otherButtonTitles:@"OK", nil]; 

其既定here說:「其他按鈕標題添加到接收器,具有零終止。」

基本上,它是一個多值參數,直到零爲止。它與數組和列表的工作方式相同。