0

我用兩個ViewController編寫了簡單的基於UINavigationController的應用程序;UINavigationController保留

在第一視圖控制器我有此代碼

-(IBAction)loadSecondView 
{ 
    SecondView *secondView = [[SecondView alloc]init]; 
    [secondView setTotal:self.total]; 
    [self.navigationController pushViewController:secondView animated:YES]; 
    [secondView release]; 
    [self.navigationController popViewControllerAnimated:YES]; 
} 

在我具有被programitacly創建標籤的第二視圖。

..... 

[self.playerNameField addTarget:self 
           action:@selector(textFieldFinished:) 
..... 

- (IBAction)textFieldFinished:(id)sender 
{ 
    [sender resignFirstResponder]; 
} 

問題是我碰到鍵盤按鈕完成後應用程序崩潰。

[SecondView performSelector:withObject:]: message sent to deallocated instance 0x522af60 

我明白什麼是問題,但我不明白爲什麼發生這種情況。

糾正我,如果我錯了。

SecondView *secondView = [[SecondView alloc]init]; // retain 1 
[self.navigationController pushViewController:playersNames animated:YES]; //secondView have retain of 2 
[secondView release]; // now secondView should have retain of 1 

回答

1

你爲什麼要推,然後在彈出navigationcontroller,一旦你推的觀點應該切換和playersName控制器,它認爲應該會出現。

此外,我不清楚爲什麼你得到secondView的實例,但從來沒有使用它。 推入navigationController應該保留控制器,但看起來SecondView永遠不會得到保留,並且一旦您使用它,先前的控制器已經釋放它。

0

我不明白這一點:

SecondView *secondView = [[SecondView alloc]init]; 
[self.navigationController pushViewController:playersNames animated:YES]; 
[secondView release]; 

你們是不是要secondView添加到導航儀?這將是這樣的:

SecondView *secondView = [[SecondView alloc]init]; 
[self.navigationController pushViewController:secondView animated:YES]; 
[secondView release];