2012-08-10 101 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说:“其他按钮标题添加到接收器,具有零终止。”

基本上,它是一个多值参数,直到零为止。它与数组和列表的工作方式相同。