UIAlertView退出EXC_BAD_ACCESS错误 [英] UIAlertView exits EXC_BAD_ACCESS error

查看:86
本文介绍了UIAlertView退出EXC_BAD_ACCESS错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的错误:当我单击navigationbar.backItemButton时,我正在显示带有两个按钮的UIAlertView.当我按其中任何一个时,应用程序仅以EXC_BAD_ACCESS终止.方法-不调用(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex.我该如何解决?谢谢!

I have such error: when I click navigationbar.backItemButton I'm showing UIAlertView with two buttons. When I press on any of them application terminates just with EXC_BAD_ACCESS. Method - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex don't called. How can I solve it? Thanx!

//h-文件

@interface DetailsTableViewController : UITableViewController <UITextFieldDelegate, UIAlertViewDelegate>

//m-文件

- (void)viewWillDisappear:(BOOL)animated
{
    //if changes unsaved - alert reask window
    if (isDirty)
    {
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Save changes?"  
                                                      message:@"Press YES if you want to save changes before exit, NO - other case."  
                                                     delegate: self  
                                                    cancelButtonTitle: @"NO"  
                                                    otherButtonTitles: @"YES", nil];   

        [message show];  

        [message autorelease];
    }   
}

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex: buttonIndex];  

    if([title isEqualToString: @"YES"])  
    {  
        [self saveBtnUserClick];    
    } 
}

推荐答案

我认为问题是,在您点击后退按钮后,当前控制器将从导航堆栈中删除并释放,因此当Alert尝试调用其委托方法时,它将调用它们在释放对象上导致EXC_BAD_ACCESS错误.要解决该问题,我看到2个明显的选项(尽管可能有更好的解决方案):

I think the problem is that after you tapped back button your current controller is removed from navigation stack and deallocated, so when alert tries to call its delegate methods it calls them on deallocated object which results in EXC_BAD_ACCESS error. To workaround the problem I see 2 obvious options (although there may be better solutions):

  1. 额外将控制器保留在某个位置(可能是以前的控制器中的某个位置),但是您需要找到一种在完成后释放它的方法.
  2. 创建您的自定义按钮,而不是标准的后退"按钮,并在点击时显示警报.然后在警报的委托方法中,从导航堆栈中弹出当前控制器.

这篇关于UIAlertView退出EXC_BAD_ACCESS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆