我用兩個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