UIViewController 不保留其以编程方式创建的 UISearchDisplayController
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也遇到过同样的事情。我以编程方式创建所有控制器/视图。一切都工作正常,直到我将我的项目转换为使用 ARC。一旦我这样做了,
UISearchDisplayControllers
就不再保留,并且每个UIViewController
中的searchDisplayController
属性在运行循环结束后为零。我不知道为什么会发生这种情况。 Apple 文档建议视图控制器应保留 SDC,但这显然没有发生。
我的解决方案是创建第二个属性来保留 SDC,并在卸载视图时将其清零。如果您不使用 ARC,则需要在
viewDidUnload
和dealloc
中释放mySearchDisplayController
。否则就这样就好了。在 MyViewController.h 中:
在 MyViewController.m 中:
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 thesearchDisplayController
property in eachUIViewController
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
inviewDidUnload
anddealloc
. Otherwise this is good as is.In MyViewController.h:
In MyViewController.m:
上面的解决方案工作得很好,但我还发现您可以
在 UIViewController 子类的上下文中使用。
The solution above works just fine, but I also found that you can use
in the context of a
UIViewController
subclass.