UIPopoverController、UIActionSheet 和模态视图控制器的保留/释放模式?
我不太清楚以下实例所需的对象所有权模式。当我的 UIViewController 将弹出窗口控制器、操作表或另一个视图控制器作为模式显示时,我是否需要保留对该子控制器的保留引用,直到它被解除为止?
换句话说,以下代码行是否有效地“转移”所有权?
[aPopoverController presentPopoverFromBarButtonItem:someButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
[anActionSheet showFromBarButtonItem:someButtonItem animated:NO];
[aViewController presentModalViewController:someOtherViewController animated:YES];
有人可以向我指出有关此主题的明确文档吗?
I'm somewhat unclear on the object ownership patterns required for the following instances. When my UIViewController presents a popover controller, an action sheet, or another view controller as modal, am I required to hang onto a retained reference to that child controller until it's been dismissed?
In other words, do the following lines of code effectively "transfer" ownership, or not?
[aPopoverController presentPopoverFromBarButtonItem:someButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
[anActionSheet showFromBarButtonItem:someButtonItem animated:NO];
[aViewController presentModalViewController:someOtherViewController animated:YES];
Can someone point me to explicit documentation on this subject?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UIPopoverViewController 的内存管理/拥有略有不同。呈现弹出窗口不保留内存,因此您无法将 popviewcontroller 的所有权转移给呈现对象。
为了避免内存泄漏,您必须采用 UIPopoverControllerDelegate 并实现 DidDismissPopOver 方法,如下所示:
这样,您可以安全分配并呈现 PopOver:
UIPopoverViewController has a slight different memory management/owning. Present a popover does not retain the memory, so you can't transfer the ownership of your popviewcontroller to the presenting object.
To avoid memory leak, you have to adopt the UIPopoverControllerDelegate and implement the DidDismissPopOver method as follow:
This way, you can safe alloc and present a PopOver:
呈现模式视图控制器保留了 UIViewController。这实际上从文档中并不清楚。但是,我使用以下代码对其进行了测试...
self.setupViewController 已在本地保留,但呈现它输出以下内容:
因此它可能被保留在本地 modalViewController 属性以及视图层次结构中。忽略它会平衡这些。
所以底线是,如果你想直接控制它,就保留它,但你不必这样做。
编辑 - 需要明确的是,如果您将自己设置为对象的委托,则正确的模式是始终保留对象。那是因为为了安全起见,您应该在 dealloc 中将委托设置为零。但实际上,在释放之前模态控制器总是会被解除,所以这不是问题。你会注意到Apple也在[UIView setAnimationDelegate:]中打破了这条规则,它实际上保留了你设置的委托。
Presenting a modal view controller retains the UIViewController. This is actually not clear from the docs. However, I tested it using the following code...
The self.setupViewController is already retained locally, but presenting it output the following:
So it is probably being retained in the local modalViewController property, as well as in the view hierarchy. Dismissing it will balance these.
So bottom line is, retain it if you want to control it directly, but you don't have to.
EDIT - Just to be clear, the correct pattern is to always retain an object if you set yourself as its delegate. That's because you should be setting the delegate to nil in your dealloc for safety. Practically though, a modal controller is always going to be dismissed before you dealloc, so it's not an issue. You'll notice Apple also breaks this rule in [UIView setAnimationDelegate:], which actually retains the delegate you set.