UIPopoverController、UIActionSheet 和模态视图控制器的保留/释放模式?

发布于 2024-09-02 07:05:29 字数 476 浏览 9 评论 0原文

我不太清楚以下实例所需的对象所有权模式。当我的 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 技术交流群。

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

发布评论

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

评论(2

水晶透心 2024-09-09 07:05:29

UIPopoverViewController 的内存管理/拥有略有不同。呈现弹出窗口不保留内存,因此您无法将 popviewcontroller 的所有权转移给呈现对象。

为了避免内存泄漏,您必须采用 UIPopoverControllerDelegate 并实现 DidDismissPopOver 方法,如下所示:

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
    [popoverController release];
}

这样,您可以安全分配并呈现 PopOver:

-(void)showSearch:(id)sender {
    SearchViewController *searchVC = [[SearchViewController alloc] init];
    UIPopoverController *popVC = [[UIPopoverController alloc] initWithContentViewController:searchVC];
    popVC.delegate = self;
    [popVC setPopoverContentSize:CGSizeMake(320, 100)];
    [popVC presentPopoverFromRect:CGRectMake(200, 200, 320, 100) inView:self.view permittedArrowDirections:0 animated:YES];
    [searchVC release];
}

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:

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
    [popoverController release];
}

This way, you can safe alloc and present a PopOver:

-(void)showSearch:(id)sender {
    SearchViewController *searchVC = [[SearchViewController alloc] init];
    UIPopoverController *popVC = [[UIPopoverController alloc] initWithContentViewController:searchVC];
    popVC.delegate = self;
    [popVC setPopoverContentSize:CGSizeMake(320, 100)];
    [popVC presentPopoverFromRect:CGRectMake(200, 200, 320, 100) inView:self.view permittedArrowDirections:0 animated:YES];
    [searchVC release];
}
泛泛之交 2024-09-09 07:05:29

呈现模式视图控制器保留了 UIViewController。这实际上从文档中并不清楚。但是,我使用以下代码对其进行了测试...

NSLog(@"BEFORE %d", [self.setupViewController retainCount]);
[self.navigationController presentModalViewController:self.setupViewController animated:YES];
NSLog(@"AFTER %d", [self.setupViewController retainCount]);

self.setupViewController 已在本地保留,但呈现它输出以下内容:

2010-05-19 10:07:36.687 LocateMe[27716:207] BEFORE 1
2010-05-19 10:07:36.762 LocateMe[27716:207] AFTER 3

因此它可能被保留在本地 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...

NSLog(@"BEFORE %d", [self.setupViewController retainCount]);
[self.navigationController presentModalViewController:self.setupViewController animated:YES];
NSLog(@"AFTER %d", [self.setupViewController retainCount]);

The self.setupViewController is already retained locally, but presenting it output the following:

2010-05-19 10:07:36.687 LocateMe[27716:207] BEFORE 1
2010-05-19 10:07:36.762 LocateMe[27716:207] AFTER 3

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.

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