The searchController is declared in viewDidLoad()
of a TableViewController as searchController = UISearchController(searchResultsController: nil)
. When user selects a row in current table view, it navigates out to previous viewcontroller using self.navigationController?.popViewControllerAnimated(true)
. Whenever this happens i get a warning -
"Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior"
To avoid this, i am removing search controller from superview before calling popViewControllerAnimated(true)
on navigation controller.
I do not get the same warning if the search is active and a row is selected. May be because i am dismissing the search controller first then popping out to previous controller, so it works fine in this case. But if i dismiss search controller in all cases whether search is active or not, it doesn't help.
Here is the code.
//Dismiss searchController if active & Navigate to previous view controller on row selection
if self.searchController.active {
self.searchController.dismissViewControllerAnimated(true, completion: {
self.navigationController?.popViewControllerAnimated(true)
})
}
else {
//remove UISearchController from superView
self.searchController.view.superview?.removeFromSuperview()
self.navigationController?.popViewControllerAnimated(true)
}
I want to understand if this is the right approach as above in else block or should i do the same within :-
viewWillDisappear/viewDidDisappear
deinit{ if let superView = searchController.view.superview { superView.removeFromSuperview() } }
Which one is the right thing to do ?