UIAlertView在发布模式下导致崩溃

时间:2009-09-30 23:17:33

标签: objective-c iphone

如果用户进入相机时在我的应用程序中激活了“帮助”选项,我首先会显示UIAlertView以及如何拍照的提示:

-(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != [actionSheet cancelButtonIndex]) {
    NSString *selectedButtonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
    if ([selectedButtonTitle isEqualToString:@"Camera"]) {
        // If Help is activated display camera tips
        if (helpEnabled == YES) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Camera Tips" message:@"\n\n\n\n\n\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Go To Camera"];
            UIImageView *cameraHelpView = [[UIImageView alloc] initWithFrame:CGRectMake(17, 40, 250, 255)];
            UIImage *cameraTutorial = [UIImage imageNamed:@"Camera_Tips.png"];
            cameraHelpView.image = cameraTutorial;
            [alert addSubview:cameraHelpView];
                    [cameraHelpView release];       
            [alert show];
            [alert release];
        }
    }
}

这在调试模式下工作,但在发布模式下导致“EXC BAD ACCESS”错误。从这一点开始,我可以模态地呈现一个新的视图控制器,但UIAlertView将始终使应用程序崩溃。为什么呢?

2 个答案:

答案 0 :(得分:0)

我不知道为什么它在调试模式下工作,但看起来你正在释放你的cameraHelpView,但仍在使用它。在alert的子视图中是指向cameraHelpView的指针;当您释放它时,它将无法再被访问。我建议您使用-[NSObject release]替换此上下文中的所有-[NSObject autorelease]来电。因此:

UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Camera Tips" message:@"\n\n\n\n\n\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Go To Camera"] autorelease];
UIImage *cameraTutorial = [UIImage imageNamed:@"Camera_Tips.png"];
UIImageView *cameraHelpView = [[[UIImageView alloc] initWithFrame:CGRectMake(17, 40, 250, 255)] autorelease];
cameraHelpView.image = cameraTutorial;
[alert addSubview:cameraHelpView];
[alert show];

继续尝试,让我知道它是否有效。祝你好运!

答案 1 :(得分:0)

我发现了自己的错误。我没有传递nil作为otherButtonTitles的最后一个参数!调试模式必须为您查看并修复此错误。希望这有助于某人。