UIAlertView exits EXC_BAD_ACCESS error

限于喜欢 提交于 2019-12-11 17:14:20

问题


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

回答1:


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.



回答2:


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




回答3:


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.




回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/4212580/uialertview-exits-exc-bad-access-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!