I have a project using StoryBoards and UISearchDisplayController
used in the context of a UINavigationController
, that appears in the root viewcont
Explicitly declare your outlet
@property (nonatomic, strong) IBOutlet UISearchDisplayController *searchDisplayController;
Then in dealloc - add these lines - nil out the delegate / data source so that they do not receive any message further when the searchDisplayController deallocates itself.
self.searchDisplayController.delegate = nil;
self.searchDisplayController.searchResultsDelegate = nil;
self.searchDisplayController.searchResultsDataSource = nil;
ViewDidUnload Called means, you got memory exceptions in your application.
First try to fix your memory issues, then automatically search display view controller problem will be resolved.
Because there seems to nothing wrong in code, When memory exceptions occurs, then better to take user previous screen by saying [self.navigationController popViewControllerAnimated:NO]
So far, I found this working solution for iOS 5 SDK using ARC:
in .h file, declare your own searchDisplayController property with IBOutlet
@property (strong, nonatomic) IBOutlet UISearchDisplayController * searchDisplayController;
Then in the .m file, synthesize it:
@synthesize searchDisplayController;
But don't set it to nil in viewDidUnload.
So that the search display controller will use the property you create instead of using the inherited property.
I also notice the similar bug also appear for gesture recognizers (if you create gesture recognizers from the storyboard instead of creating them programmablly). We also need to create STRONG gesture recognizer properties and hook them with the gesture recognizer objects that you create in storyboard. Then in viewDidUnload, don't set them to nil. <-- this prevent the crashes.
An important point to note, is that crashes like this can occur if you leave the view whilst your searchDisplayController is still active. This is the issue I was having, selecting an item in the searchDisplayController was set to pop the view controller from the stack, in order to fix this, I had to include the following code before the view was popped...
if (self.searchDisplayController.active) {
[self.searchDisplayController setActive:NO];
}
Why dont you use self.searchDisplayController only?
I have already used that many times it wont create any problem. You can also customize that if you want.
@Wayne: I had run into the same issue with a SearchDisplayController created from a Storyboard, and spend over a day trying to debug a crash that seemed to appear when none of my code was running. In my case the symptom was the user taps a tab in UITabBarController to return to a ViewController that has been unloaded after a memory warning. The unloaded view controller's viewDidLoad method never runs and the code gets at least as far as tabBarController:didSelectViewController: (which should run after viewDidLoad) before it crashes somewhere in the assembly code!
Thanks massively for posting this workaround and for all the follow-ups. A small improvement is to move your UIDisplayController instantiation to a lazily loaded accessor method for the searchDisplayController property. The practical effect is negligible but it looks nicer!