Reputation: 5074
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
Reputation: 445
"Try Changing delegate to nil instead of self. It fixed my issue." worked for me. Thanx
Upvotes: 0
Reputation: 9979
Try Changing delegate to nil instead of self. It fixed my issue.
Upvotes: 2
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):
Upvotes: 3
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
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