2011-03-05 45 views
0

嗨,我遇到了didSelectRowAtIndexPath問題,我的應用程序在選中某行時出現EXC-BAD-ACCESS錯誤。奇怪的是,我在accessoryButtonTappedForRowWithIndexPath中具有完全相同的代碼片段,並且它正確顯示了詳細信息視圖。這是此方法的代碼。didSelectRowAtIndexPath問題

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(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; 

} 
+0

請告訴我們應用程序崩潰的發佈源代碼行。並且請張貼堆棧跟蹤。否則,它只是猜測。 – Codo 2011-03-05 14:46:26

+0

發佈的代碼看起來不錯,除了可能的內存泄漏,但不是崩潰的原因。內存泄漏是_childController_實例。您可以使用此方法創建它,並在方法結束時將該變量設置爲_nil_。你應該先釋放它。爲什麼_childController_無論如何都是一個實例變量(而不是本地變量)?由於您始終將其設置爲_nil_,因此可能不會在其他地方使用。 – Codo 2011-03-05 14:48:52

+0

代碼在最後一行失敗後完成。發生錯誤時控制檯中沒有堆棧跟蹤。 – JohnB 2011-03-05 15:24:37

回答

0

嘗試替換「childController = nil;」與「[childController發佈];」

+0

這沒有工作:( – JohnB 2011-03-06 09:57:43

0

好吧,我已修復它,但不明白爲什麼它的作品。我在dealloc方法中釋放了子控制器,並在viewdidunload中將其設置爲nil,問題得到解決。任何人都可以解釋爲什麼這解決了這個問題?

+1

你的vc被釋放,但沒有設置爲零,所以在執行過程中的某個時刻,檢查[vc == nil]在不應該時返回false。 – Rog 2011-03-06 11:43:22

0

我懇求任何在應用程序中隨機崩潰的人,他們無法解釋將NSZombieEnabled添加到構建環境中,這些指南可以在線提供,並且可以節省大量時間!