UIViewController 不保留其以编程方式创建的 UISearchDisplayController

发布于 2024-12-08 07:45:23 字数 1348 浏览 0 评论 0原文

UIViewController 文档 中有关 <代码>searchDisplayController属性1 它说:

如果您以编程方式创建搜索显示控制器,则搜索显示控制器在初始化时会自动设置此属性。

当我这样创建 UISearchDisplayController 时:

[[[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self] autorelease];

-[UIViewController searchDisplayController] 不是 nil。然而,它在事件循环完成后被清空,这导致当我触摸搜索栏内部时搜索显示控制器不显示。没有任何崩溃。这很奇怪。如果我省略对 autorelease 的调用,一切正常:

[[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];

但是,泄漏了 UISearchDisplayController (我用 Instruments 验证了这一点)。由于 searchDisplayController 属性 被标记为(nonatomic,retain,readonly)我希望它在设置后保留UISearchDisplayController

这篇 stackoverflow 文章 是相关的。

In the UIViewController documentation about the searchDisplayController property 1 it says:

If you create your search display controller programmatically, this property is set automatically by the search display controller when it is initialized.

And when I create my UISearchDisplayController thusly:

[[[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self] autorelease];

-[UIViewController searchDisplayController] is not nil. However, it is nilled out after the event loop finishes, which causes the search display controller not to show when I touch inside the search bar. Nothing crashes. This is very weird. If I omit the call to autorelease, everything works:

[[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];

However, leaks the UISearchDisplayController (I verified this with Instruments). Since the searchDisplayController property is marked as (nonatomic, retain, readonly) I expect that it would retain the UISearchDisplayController after it is set.

This stackoverflow article is related.

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

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

发布评论

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

评论(2

飘然心甜 2024-12-15 07:45:23

我也遇到过同样的事情。我以编程方式创建所有控制器/视图。一切都工作正常,直到我将我的项目转换为使用 ARC。一旦我这样做了,UISearchDisplayControllers 就不再保留,并且每个 UIViewController 中的 searchDisplayController 属性在运行循环结束后为零。

我不知道为什么会发生这种情况。 Apple 文档建议视图控制器应保留 SDC,但这显然没有发生。

我的解决方案是创建第二个属性来保留 SDC,并在卸载视图时将其清零。如果您不使用 ARC,则需要在 viewDidUnloaddealloc 中释放 mySearchDisplayController。否则就这样就好了。

在 MyViewController.h 中:

@property (nonatomic, strong) UISearchDisplayController * mySearchDisplayController;

在 MyViewController.m 中:

@synthesize mySearchDisplayController = _mySearchDisplayController;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // create searchBar
    _mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    _mySearchDisplayController.delegate = self;
    _mySearchDisplayController.searchResultsDataSource = self;
    _mySearchDisplayController.searchResultsDelegate = self;
    // other stuff
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    _mySearchDisplayController = nil;
    // other stuff
}

I've run into the same thing. I create all of my controllers/views programmatically. Everything was working fine until I converted my project to use ARC. Once I did the UISearchDisplayControllers were no longer retained and the searchDisplayController property in each UIViewController was nil after the run loop ended.

I don't have an answer why this is happening. The Apple docs suggest that the SDC should be retained by the view controller but this is clearly not happening.

My solution was to create a second property to retain the SDC and I nil it when I unload the view. If you are not using ARC you need to release mySearchDisplayController in viewDidUnload and dealloc. Otherwise this is good as is.

In MyViewController.h:

@property (nonatomic, strong) UISearchDisplayController * mySearchDisplayController;

In MyViewController.m:

@synthesize mySearchDisplayController = _mySearchDisplayController;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // create searchBar
    _mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    _mySearchDisplayController.delegate = self;
    _mySearchDisplayController.searchResultsDataSource = self;
    _mySearchDisplayController.searchResultsDelegate = self;
    // other stuff
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    _mySearchDisplayController = nil;
    // other stuff
}
歌入人心 2024-12-15 07:45:23

上面的解决方案工作得很好,但我还发现您可以

[self setValue:mySearchDisplayController forKey:@"searchDisplayController"]

在 UIViewController 子类的上下文中使用。

The solution above works just fine, but I also found that you can use

[self setValue:mySearchDisplayController forKey:@"searchDisplayController"]

in the context of a UIViewController subclass.

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