基本使用UIAlertView中会导致EXC_BAD_ACCESS

问题描述:

我采取一个非常简单的iOS应用程序只是练显示弹出警报,我得到一个错误,当我按下alert按钮:基本使用UIAlertView中会导致EXC_BAD_ACCESS

线程1:EXC_BAD_ACCESS (码= 1,地址= 0x676f6f57)

这是代码:

- (IBAction)AlertButton { 


    alert = [[UIAlertView alloc] 
     initWithTitle:@"Alert" message:@"Alert" 
     delegate:self 
     cancelButtonTitle:@"Dismiss" 
     otherButtonTitles:@"Apple", "Google" ,nil]; 
    [alert show];} 


-(void)alertView :(UIAlertView *)alertView clickedButttonAtIndex:(NSInteger)buttonIndex{ 

    if(buttonIndex == 1){ 
     [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://apple.com"]]; 
    } 
    if(buttonIndex == 2){ 
     [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://google.com"]]; 
    }} 
+2

请不要张贴链接到您的整个项目。如果你不能足够简洁地描述你的问题以适应帖子内容,那么你的问题不适合堆栈溢出。 – 2013-02-21 19:40:29

+0

我相信问题是你的button = AlertButton的名称,IBaction的名称也是AlertButton。上述代码中的UIAlertView没有任何问题。 – 2013-02-21 19:44:16

问题与UIAlertView的构造,在该行:

otherButtonTitles:@"Apple", "Google" ,nil]; 

你忘了@之前"Google"。最后修改:

-(void)alertView :(UIAlertView *)alertView clickedButttonAtIndex:(NSInteger)buttonIndex{ 

通过

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 
+1

“此外,你必须在你的类上声明协议UIAlertViewDelegate的采用” - 不,你不需要,它只是编译器的提示。 – 2013-02-21 19:59:59

+0

是的,你是对的,在这种情况下没有必要,我修复了答案 – tkanzakic 2013-02-21 20:03:46

+1

谢谢。 (不是我主张避免它,因为这样做是很好的做法,只是为了记录 - 人们经常会忘记Objective-C是一种高度动态的语言,声明事关重大。) – 2013-02-21 20:04:48

真正的问题是在你的前会错过的,所以它不是NSString,因此崩溃。

使用本 .H

不需要IBOutlet.just

UIAlertView *alert; 

.M

alert = [[UIAlertView alloc] 
     initWithTitle:@"Alert" 
       message:@"Alert" 
      delegate:self 
    cancelButtonTitle:@"Dismiss" 
    otherButtonTitles:@"Apple", @"Google", nil 
]; 
+0

非常感谢你:) – Rayan 2013-02-21 20:38:37