yozhik
yozhik

Reputation: 5074

UIAlertView exits EXC_BAD_ACCESS error

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

Upvotes: 0

Views: 2470

Answers (5)

Vinicius Souza
Vinicius Souza

Reputation: 445

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

Upvotes: 0

Harikrishnan
Harikrishnan

Reputation: 9979

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

Upvotes: 2

Vladimir
Vladimir

Reputation: 170839

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.

Upvotes: 3

Amitesh Kumar
Amitesh Kumar

Reputation: 46

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.

Upvotes: 1

Bourne
Bourne

Reputation: 10312

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.

Upvotes: 1

Related Questions