didSelectRowAtIndexPath Problem
Hi i am having a problem with didSelectRowAtIndexPath where my app crashes with EXC-BAD-ACCESS when a row is selected. The strange this is that i have the exact same piece of code in the accessoryButtonTappedForRowWithIndexPath and it correctly displays the detail view. Here is the code for this method.
-(void)tableView:(UITableView *)tableView didSe开发者_如何学JAVAlectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
if(childController == nil)
{
childController = [[ScoreCardDetailViewController alloc]initWithNibName:@"ScoreCardDetailView" bundle:nil];
}
ScoreCard *selectedScoreCard = [self.list objectAtIndex:row];
if([self checkPlusMinus:selectedScoreCard.score]==0||[self checkPlusMinus:selectedScoreCard.score]==1)
{
childController.title = [NSString stringWithFormat:@"%@ +%d",selectedScoreCard.course.courseName, selectedScoreCard.score];
childController.selectedScoreCard = selectedScoreCard;
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"Back" style: UIBarButtonItemStyleBordered target: nil action: nil];
[[self navigationItem] setBackBarButtonItem: newBackButton];
[newBackButton release];
[self.navigationController pushViewController:childController animated:YES];
}
else
{
childController.title = [NSString stringWithFormat:@"%@ %d",selectedScoreCard.course.courseName, selectedScoreCard.score];
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"Back" style: UIBarButtonItemStyleBordered target: nil action: nil];
[[self navigationItem] setBackBarButtonItem: newBackButton];
[newBackButton release];
childController.selectedScoreCard = selectedScoreCard;
[self.navigationController pushViewController:childController animated:YES];
}
childController=nil;
}
try replacing "childController=nil;" with "[childController release];"
Ok i have fixed it but can not understand why it works. I released the child controller in the dealloc method and set it to nil in the viewdidunload and the problem was fixed. Can anyone shed any light on why this fixed the problem?
I implore anyone who has random crashes in their app they can't explain to add NSZombieEnabled into the build environment, there are guides to do this available online and it will save you loads of time!
精彩评论