向 UINavigationController 对象发布消息

发布于 2024-09-10 02:49:17 字数 1280 浏览 3 评论 0原文

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

在本例中,我使用 alloc 消息创建 UINavigationController,并使用 UIView 控制器对其进行初始化。然后,我通过将视图传递给presentModalViewController 方法来以模式方式呈现视图。代码如下:

- (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 的特定分配/初始化消息的工作方式是否与其他类中的工作方式不同,即?它可能返回一个自动释放的实例吗?

谢谢!

彼得

I am relatively new to Cocoa programming, and some aspects of memory managing are still troubling me.

In this case, I am creating a UINavigationController using the alloc message, and initialising it with a UIView controller. Then, I am presenting the view modaly by passing it to the presentModalViewController method. Below is the code:

- (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];
}

According to the retain count rules, if I send the message "alloc" to a class, an instance of that class is returned with retain count 1, and I am responsible for releasing it. In the above code, I release the newPropertyNavigationController instance after passing it to the modalViewController and presenting it. When I dismiss the modal view, the app crashes.

If I comment out the last line, the app doesn't crash.

Why is this happening? Is the particular alloc/init message to the UINavigationController working differently to the way it works in other classes, ie. is it perhaps returning an autoreleased instance?

Thanks!

Peter

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

格子衫的從容 2024-09-17 02:49:17

您需要停止正在做的事情并通读 这个。内存管理的规则非常简单易懂。让它们铭刻在你的脑海中(不需要很长时间)。然后逐行检查您的问题点代码以及从您的代码调用的 api。当规则在你脑海中清晰可见时,以这种方式跟踪你的代码,将帮助你解决你的记忆问题,并且也许,只是也许,可以帮助你防止将来再犯这些问题。

You need to stop what you're doing and read through this. The rules for memory management are very simple and understandable. Let them be burnt into your head (doesn't take long). Then examine your code line by line in your trouble spots, and apis that get called into from your code. Tracing through your code this way while the rules are fresh in your head, will help you fix your memory problems, and maybe, just maybe, help you prevent from making them in the future.

琉璃繁缕 2024-09-17 02:49:17

创建模态视图控制器的方式看起来是正确的。检查模态视图控制器上 dealloc 的实现,看看问题是否在那里。

如果您错误地删除了内存,这将解释为什么只有在释放模态视图控制器时才会出现错误。

作为参考,我发现以下 autorelease 的使用更具可读性和可维护性

NewPropertyViewController *newProperty = [[[NewPropertyViewController alloc] 
                                      initWithDictionary] autorelease];
newProperty.editProperty = [fetchedResultsController objectAtIndexPath:indexPath];
UINavigationController *newPropertyNavigationController = [[[UINavigationController 
                                      alloc]
                                      initWithRootViewController:newProperty] autorelease];
[newProperty setPropertyDelegate:self];
[self presentModalViewController:newPropertyNavigationController animated:YES];

The way you create the modal view controller looks correct. Check the implementation of dealloc on your modal view controllers to see if the problem lies there.

If you are incorrectly deleting memory there it would explain why you only get the error when you release the modal view controller.

For reference I find the following use of autorelease much more readable and maintainable

NewPropertyViewController *newProperty = [[[NewPropertyViewController alloc] 
                                      initWithDictionary] autorelease];
newProperty.editProperty = [fetchedResultsController objectAtIndexPath:indexPath];
UINavigationController *newPropertyNavigationController = [[[UINavigationController 
                                      alloc]
                                      initWithRootViewController:newProperty] autorelease];
[newProperty setPropertyDelegate:self];
[self presentModalViewController:newPropertyNavigationController animated:YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文