UINavigationController保留

时间:2012-03-07 09:11:06

标签: objective-c ios memory-management uinavigationcontroller retain

我用两个ViewControllers编写了简单的基于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

2 个答案:

答案 0 :(得分:1)

为什么你推动然后弹出导航控制器,一旦你推动视图应该切换和playerName控制器及其视图应该出现。

另外我不清楚为什么你得到secondView的实例但从不使用它。 推送到navigationController应保留控制器,但似乎SecondView永远不会被保留,一旦你使用它,之前的控制器已经释放它。

答案 1 :(得分: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];