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.
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
}