gpt4 book ai didi

ios - 向 UINavigationController 对象发布消息

转载 作者:行者123 更新时间:2023-12-01 19:29:13 25 4
gpt4 key购买 nike

我对 Cocoa 编程比较陌生,内存管理的某些方面仍然困扰着我。

在这种情况下,我正在使用 alloc 消息创建 UINavigationController,并使用 UIView Controller 对其进行初始化。然后,我通过将 View 模式传递给 presentModalViewController 方法来呈现 View 模式。下面是代码:

- (void)tableView:(UITableView *)tableView 
accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Tapped on disclosure button");
NewPropertyViewController *newProperty = [[NewPropertyViewController alloc]
initWithDictionary];
newProperty.editProperty = [fetchedResultsController objectAtIndexPath:indexPath];
UINavigationController *newPropertyNavigationController = [[UINavigationController
alloc]
initWithRootViewController:newProperty];
[newProperty setPropertyDelegate:self];
[self presentModalViewController:newPropertyNavigationController animated:YES];

[newProperty release];
[newPropertyNavigationController release];
}

根据保留计数规则,如果我向一个类发送消息“alloc”,则返回该类的一个实例,保留计数为 1,我负责释放它。在上面的代码中,我在将 newPropertyNavigationController 实例传递给 modalViewController 并呈现它之后释放它。当我关闭模态视图时,应用程序崩溃。

如果我注释掉最后一行,应用程序不会崩溃。

为什么会这样?到 UINavigationController 的特定 alloc/init 消息的工作方式是否与它在其他类中的工作方式不同,即。它是否可能返回一个自动释放的实例?

谢谢!

彼得

最佳答案

你需要停下手头的事,通读this .内存管理的规则非常简单易懂。让它们被烧到你的头上(不需要很长时间)。然后在您的问题点逐行检查您的代码,以及从您的代码中调用的 api。在规则内存犹新时以这种方式跟踪代码,将帮助您解决内存问题,并且也许,只是也许,帮助您防止将来出现这些问题。

关于ios - 向 UINavigationController 对象发布消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3278532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com