UINavigationController - 何时释放推送的视图控制器等

发布于 2024-10-04 22:01:18 字数 885 浏览 2 评论 0原文

对大块“o”文本表示歉意,但我有点像初学者,正在尝试使用 UINavigationController 作为我正在编写的应用程序的一部分,我只是想知道应该如何处理添加视图控制器UINavigationController 以及何时(如果有的话)我应该释放它们。

在我看来,添加视图控制器时有两种可能的方法:

  1. 在管理类中为每个视图控制器都有一个实例变量。在将其推送到导航控制器之前对其进行初始化(如果实例变量为零)。如果视图控制器不为 nil,则随后在管理类的 dealloc 方法中释放该视图控制器。

  2. 将视图控制器创建为方法中的局部变量,并将其推送到导航控制器上,然后立即释放它。

但是,我不确定这些的有效性。 (我的直觉是采用实例变量方法,但这也许是由于我缺乏理解。)

再深入一点,我怀疑我的困惑与我不太明白是否或不负责处理被推入导航控制器堆栈的视图控制器,或者当它们从视图控制器堆栈中弹出时是否会自行处理它们。 (例如:当用户点击“后退”按钮时。)

如果是前者,那么我可以看到我需要使用实例变量方法并在管理对象中实现 UINavigationControllerDelegate 方法来确定何时应该释放和nil 每个视图控制器等。

最后(为了来世的奖励积分)你如何轻松判断一个方法是否会增加其参数之一的保留计数? (文档中似乎没有任何线索,但也许我遗漏了一些明显的东西。)例如,当我使用以下内容时......

[[self navigationController] pushViewController:exampleVC animated:YES];

这会增加视图上的保留计数控制器? (再一次,我的直觉是它应该,但是在这些危险时期,这和巧克力防火剂一样有用。)

提前致谢。

Apologies for the big block 'o' text, but I'm somewhat of a beginner having a play around with using a UINavigationController as a part of an app I'm writing and I'm just wondering how I should handle adding view controllers to a UINavigationController and when (if ever) I should release them.

As I see it, there are two potential approaches when adding a view controller:

  1. Have an instance variable for each view controller in the managing class. Initialise it (if the instance variable is nil) prior to pushing it onto the nav controller. Subsequently release the view controller in the dealloc method of the managing class if it isn't nil.

  2. Create the view controller as a local variable within a method and push it onto the navigation controller prior to immediately releasing it.

However, I'm unsure of the validity of each of these. (My gut feel is to go with the instance variable approach, but perhaps that's due to a lack of understanding on my part.)

Digging a bit deeper, I suspect that my confusion is related to the fact that I don't quite grasp whether or not I'm responsible for disposing of the view controllers that are pushed onto the navigation controller stack, or whether it'll dispose of them itself when they're popped off its view controller stack. (e.g.: When the user hits the 'back' button.)

If it's the former, then I can see that I'll need to use the instance variable approach and implement the UINavigationControllerDelegate methods in the managing object to determine when I should release and nil each view controller, etc.

Finally (for bonus points in the afterlife) how can you easily tell if a method will increment the retain count of one of its parameters? (There don't appear to be any clues in the documentation, but perhaps I'm missing something obvious.) For example, when I use something along the lines of...

[[self navigationController] pushViewController:exampleVC animated:YES];

...will this increase the retain count on the view controller? (Once again, my gut feel is that it should, but that's about as much use as a chocolate fireguard in these dangerous times, etc.)

Thanks in advance.

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

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

发布评论

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

评论(1

九局 2024-10-11 22:01:18

这很简单。如果您将一个对象交给另一个对象,而另一个对象用它做一些不受您直接控制的事情,它会保留您传递的对象(几乎所有事情都是这样工作的)。

例如:所有 NSArray/Dictionary/Set 集合都会保留您的对象,因为如果您要释放此类记录中包含的对象,则某些条目将无效并且集合将不知道。

对于导航控制器和此类事物来说,情况完全相同,因为您不知道视图控制器是否以及何时显示或丢弃。

That's pretty simple. If you hand off an object to another object that does something with it that's not under your direct control it retains the object you pass (pretty much everything works like that).

For Example: All NSArray/Dictionary/Set Collections retain your objects because if you would deallocate a object contained in such a record some entries would be invalid and the collection would not know.

Exactly the same is true for navigation controllers and such things, as you don't know if and when your viewcontroller is shown or discarded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文