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.
The solution above works just fine, but I also found that you can use
in the context of a
UIViewController
subclass.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: