In the UIViewController
documentation about the searchDisplayController
property 1 it says:
If you create your search displa
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.
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
}