UIAlertView退出EXC_BAD_ACCESS错误

发布于 2024-10-02 17:13:10 字数 1313 浏览 0 评论 0原文

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

//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];    
    } 
}

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 - file

@interface DetailsTableViewController : UITableViewController <UITextFieldDelegate, UIAlertViewDelegate>

//m - file

- (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];    
    } 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

挽容 2024-10-09 17:13:10

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

  1. 将控制器额外保留在某个地方(可能在以前的控制器中),但是您需要找到在完成后释放它的方法。
  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. Extra retain your controller somewhere (in previous controller may be), but you need to find way to release it when you're done.
  2. Create your custom button instead of standard "back" and just show alert when it tapped. Then in alert's delegate method pop your current controller from navigation stack.
忘你却要生生世世 2024-10-09 17:13:10

尝试将 delegate 更改为 nil 而不是 self。它解决了我的问题。

Try Changing delegate to nil instead of self. It fixed my issue.

℉服软 2024-10-09 17:13:10

您的视图控制器是否实现了 UIAlertViewDelegate?如果没有,请在 { 开始之前添加接口声明。

还可以在 clickedButtonAtIndex 方法中尝试 NSLogging 并打印 buttonIndex 值并查看控制台。

编辑:再次阅读您的帖子,我猜您确实错过了界面声明中的 UIAlertViewDelegate 。

Is your view controller implementing the UIAlertViewDelegate? If not, add in you interface declaration before the { starts.

Also try NSLogging inside the clickedButtonAtIndex method and print the buttonIndex values and see the console.

Edit: Reading your post again, I guess you indeed have missed the UIAlertViewDelegate in your interface declaration.

尽揽少女心 2024-10-09 17:13:10

可能是[消息自动释放];
你用错了吗
【消息发布】;

因为你已经使用了 [[UIAlertView alloc] init.....];你应该释放内存。

autorelease 可以与内存依赖于编译器的结构一起使用,或者您没有手动给出内存。

享受。

Probably [message autorelease];
is you mistake use
[message release];

Because you have used [[UIAlertView alloc] init.....]; there for you should release the memory.

autorelease is something will work with the structure which memory is compiler dependent or you have not given the memory manually.

Enjoy.

风为裳 2024-10-09 17:13:10

“尝试将 delegate 更改为 nil 而不是 self。它解决了我的问题。”为我工作。谢谢

"Try Changing delegate to nil instead of self. It fixed my issue." worked for me. Thanx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文