0

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 :-

  1. viewWillDisappear/viewDidDisappear
  2. deinit{ if let superView = searchController.view.superview { superView.removeFromSuperview() } }

Which one is the right thing to do ?

INDIA IT TECH
  • 1,902
  • 4
  • 12
  • 25
CuriousDev
  • 301
  • 3
  • 16

1 Answers1

0

I think first one is better option that check for active before removing it from its superview.