Canceling a search using the UISearchController fails

In our application, we have a UITableViewController with a UISearchController:

searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; self.searchController.searchResultsUpdater = self; [self.searchController.searchBar sizeToFit]; self.tableView.tableHeaderView = self.searchController.searchBar; self.showFooterView = YES; self.searchController.delegate = self; self.searchController.dimsBackgroundDuringPresentation = NO; // default is YES self.searchController.searchBar.delegate = self; self.definesPresentationContext = YES; 

The table view controller is also UISearchBarDelegate and UISearchControllerDelegate.

 #pragma mark - UISearchBarDelegate - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { [searchBar resignFirstResponder]; } - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { self.contacts = self.allContacts; [self.tableView reloadData]; } 

Now everything works as expected, but there are times when the user starts the search, enters several characters in the search bar, the results are returned, and the user cancels the search, and then this happens:

 Fatal Exception: NSInvalidArgumentException -[_UIFullscreenPresentationController adaptivePresentationController]: unrecognized selector sent to instance 0x147c81ce0 Fatal Exception: NSInvalidArgumentException 0 CoreFoundation 0x1842b4f48 __exceptionPreprocess 1 libobjc.A.dylib 0x198d77f80 objc_exception_throw 2 CoreFoundation 0x1842bbc5c __methodDescriptionForSelector 3 CoreFoundation 0x1842b8c00 ___forwarding___ 4 CoreFoundation 0x1841bccac _CF_forwarding_prep_0 5 UIKit 0x18a1ba084 -[UISearchController _searchPresentationController] 6 UIKit 0x189e7d10c -[_UISearchControllerTransplantSearchBarAnimator animateTransition:] 7 UIKit 0x189b9fa90 __56-[UIPresentationController runTransitionForCurrentState]_block_invoke 8 UIKit 0x189af856c _runAfterCACommitDeferredBlocks 9 UIKit 0x189b054bc _cleanUpAfterCAFlushAndRunDeferredBlocks 10 UIKit 0x189839984 _afterCACommitHandler 11 CoreFoundation 0x18426bbd0 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ 12 CoreFoundation 0x184269974 __CFRunLoopDoObservers 13 CoreFoundation 0x184269da4 __CFRunLoopRun 14 CoreFoundation 0x184198ca0 CFRunLoopRunSpecific 15 GraphicsServices 0x18f3d4088 GSEventRunModal 16 UIKit 0x1898b0ffc UIApplicationMain 

We have never been able to reproduce this error, although it receives information from Fabric about production.

This problem looks something like this: The cell selection after the search does not appear visually, but it loads the next Swift Xcode view , but there is no answer to this question yet.

I began to study presentation controllers, but we do not have special functions that will require presentation controller settings in a specific way.

Any ideas on how to fix this?

thanks

+4
source share
3 answers

I faced the same problem in Swift.

The problem is that the Searchbarcontroller is still holding the link (delegate) of your ViewController.

So, all you have to do is manually remove the link when viewing dealloc or disappear

Something like that:

 - (void)dealloc { self.searchController.searchResultsUpdater = nil; self.searchController.searchBar.delegate = nil; self.searchController.delegate = nil; self.searchController = nil; } 
+3
source

Try to clear all your links as follows.

 -(void)dealloc{ if (_searchController) { _searchController.searchResultsUpdater = nil; _searchController.searchBar.delegate = nil; _searchController.delegate = nil; } } 
0
source

Use the UISearchContainerViewController as the main ViewController and wrap your SearchController inside it.

A view controller that controls the presentation of search results in your interface.

Although you can present a search controller object modally, you should never push it onto the navigation controller stack or use it as a child of another container view controller. Instead, insert an instance of this class and let it control the presentation of the content of the search controllers.

https://developer.apple.com/documentation/uikit/uisearchcontainerviewcontroller

0
source

Source: https://habr.com/ru/post/1263305/


All Articles