UISearchDisplayController causes crash after viewDidUnload

瘦欲@ 提交于 2019-11-28 20:38:30

Here is what I did (granted, it's a workaround and not a fix for Apple's bug):

First, in a base UIViewController I created a property called searchController:

@property (nonatomic, retain) IBOutlet UISearchDisplayController* searchController;

I added a UISearchBar in through interface builder so that I have a placeholder in my UI for it. Then, in my viewDidLoad I manually setup the controller and wire it up:

UISearchDisplayController* searchController = [[UISearchDisplayController alloc] 
                             initWithSearchBar:self.searchBar contentsController:self];
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
searchController.delegate = self;

self.searchController = searchController;
[searchController release];

In my viewDidUnload I make sure to clear it out:

self.searchController = nil;

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.

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!

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];

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!