在代码中创建时 UISearchDisplayController 不工作?
我正在开发一个选项卡栏应用程序,其中一个选项卡有一个连接到 UISearchBar 的 UISearchDisplayController。所有这些都已连接到 NIB 中并且正在工作。当我点击搜索栏时,“范围”和“取消”按钮会飞入等,并且搜索委托会正确更新结果表。
但是,我尝试在 viewDidLoad 消息而不是 NIB 中实现相同的代码,但是当我从 NIB 中删除搜索显示控制器并取消注释我的代码以在函数中创建相同的控制器时,它不起作用。就好像没有建立一些基本连接,因此我的所有搜索委托功能都没有被调用。
这是我的搜索显示控制器的工作 NIB 版本。它连接到搜索栏、UINavigationController
子类 (MASearchController
),其根视图连接为 searchContentsController
。
替代文字 http://img192.imageshack.us/img192/3050/screenshot20100307at304.png
现在这是您希望在代码中做什么来创建相同的内容,对吗?我正在做的是将 UISearchBar 保留在 NIB 中,以便在代码中一次消除一个难题。
// [MASearchController viewDidLoad]
UISearchDisplayController *searchController = [[[UISearchDisplayController alloc]
initWithSearchBar:searchBar
contentsController:[[self viewControllers] objectAtIndex:0]] autorelease];
[searchController setDelegate:self];
[searchController setSearchResultsDelegate:self];
[searchController setSearchResultsDataSource:self];
我已经在运行时检查了所有对象,并且它们都已签出。本质上,我已经从 NIB 中删除了搜索显示控制器,然后在 viewDidLoad
消息中放入代码来创建它。
为什么这不起作用?搜索键盘出现,但我的搜索和按钮动画功能都不起作用???
I'm working on a tab bar application and one of the tabs has a UISearchDisplayController hooked up to a UISearchBar. It's all connected up in the NIB and is working. When I tap the search bar, the Scope and Cancel buttons fly in etc, and the search delegate updates the results table correctly.
However, I'm trying to implement the same code in the viewDidLoad
message instead of the NIB, however when I delete the search display controller from the NIB and uncomment my code to create the same controller in the function, it doesn't work. It's as if there's some fundamental connection not being made so that all my search delegate functionality isn't being called.
Here's my working NIB version of the Search Display Controller. It's hooked up to the search bar, the UINavigationController
subclass (MASearchController
) and the root view of that is hooked up as the searchContentsController
.
alt text http://img192.imageshack.us/img192/3050/screenshot20100307at304.png
Now this is what you would expect to do in code to create the same, right? What I'm doing is leaving the UISearchBar in the NIB to eliminate one piece of the puzzle at a time in code.
// [MASearchController viewDidLoad]
UISearchDisplayController *searchController = [[[UISearchDisplayController alloc]
initWithSearchBar:searchBar
contentsController:[[self viewControllers] objectAtIndex:0]] autorelease];
[searchController setDelegate:self];
[searchController setSearchResultsDelegate:self];
[searchController setSearchResultsDataSource:self];
I've checked all objects at run time and they all check out. Essentially I've deleted the search display controller from the NIB and then put in the code to create it in the viewDidLoad
message.
Why would this not work? The search keyboard comes up but none of my search and button animation functionality work???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
哇,我刚刚发现问题了。
我想,因为 UIViewController 的 searchDisplayController 属性是在 initWithSearchBar:contentsController: 消息中设置的,所以我仍然会自动释放我的指针副本,但是当我删除
autorelease
时,愚蠢的事情开始起作用。嘎啊。为什么它不保留自己的副本(UIViewController)?Wow, I just figured out the problem.
I figured, because the searchDisplayController property for the UIViewController is set inside the initWithSearchBar:contentsController: message I would still autorelease my copy of the pointer, but when I removed the
autorelease
the stupid thing started working. Gaaaah. Why would it not retain its own copy (the UIViewController)?通常子对象不应该保留其父对象。在这种情况下,父控制器应该保留子控制器(即搜索显示控制器)。
当您在 NIB 文件中创建 SDC 时,此操作已自动完成,因为它已添加到视图控制器的
searchDisplayController
属性中,因此在视图控制器的生命周期内保留。然而,由于在视图控制器上设置
searchDisplayController
属性考虑使用私有 api< /a>.您应该只添加一个 ivar 来保留它并在 dealloc 上手动释放它。简单地删除
autorelease
调用会导致内存泄漏,因为您将init
编辑的对象留在周围而没有保存对其的引用,所以我不认为这是正确的答案。相反,您应该将
searchController
正确保留在 ivar 中,并在 dealloc 上正确释放它,就像您希望在视图的生命周期内保持活动状态的任何对象一样控制器。Usually child objects shouldn't retain its parent. In this case its the parent controller that should retain the child (which is the search display controller).
This has been done for you automatically when you're creating the SDC in NIB file because it has been added to the view controller's
searchDisplayController
property and thus retained for the view controller's lifetime.However since setting the
searchDisplayController
property on a view controller is considered usage of private api. You should just add an ivar to retain it and release it on dealloc manually.Simply removing the
autorelease
call is a memory leak as you're leaving around an object youinit
-ed without saving a reference to it so I don't think it is the correct answer.Instead you should retain the
searchController
properly in an ivar and release it on properly on dealloc as you would any object you want to stay alive for the lifetime of the view controller.