I get an error with the following code. Essentially the app confirms calling the number when it is selected from the table view. The EXC_BAD_ACCESS is coming in when the ViewContoller with this alert is dismissed.
It only happens if the alert is triggered. It does not if the table is only viewed with no selection. That tells me I am doing something wrong with this UIAlertView. Probably having to do with memory management and releasing something I should not be.
Where am I going wrong?
phoneAlert = [[[UIAlertView alloc] initWithTitle:locationName message:displayNumber delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Call",nil] autorelease];
[phoneAlert show];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)button开发者_JAVA百科Index {
if (buttonIndex == 1) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",dialNumber]]];
}
}
- (void)dealloc {
[locations release];
[location release];
[dialNumber release];
[phoneAlert release];
[super dealloc];
}
You are assigning phoneAlert
to an autoreleased UIAlertView
, which isn't being retained by your instance because you're not using dot syntax or the setter methods, you're doing straight assignment.
So, if you defined phoneAlert
as a property with the retain
keyword, then you should do this to get the desired result:
self.phoneAlert = ...
or
[self setPhoneAlert:...];
Otherwise you will get EXC_BAD_ACCESS
in your dealloc
method, because you autorelease
d the alert view, so the instance was deallocated by the autorelease pool. Turn on zombies in your project to see this in action.
精彩评论