0

I am using searchBar and when I select to search , it workes just fine (besides this runtime warning that I can't fix Attempting to load the view of a view controller while it is deallocating... UISearchController)

But if the searchBar isActive and I press "Back" from the NavBar , the parentView is presented but the searchBar from the previous screen is also visible. I tried dismissing the searchBar if the "Back" button is pressed but it is still visible for some time. I am thinking about

self.navigationItem.backButtonItem.enabled = false  

while searchBar is active , but I don't like this solution

my code (I also have func updateSearchResultsForSearchController) :

class ViewController: UIViewController ,UISearchResultsUpdating {
var resultSearchController = UISearchController()

override func viewDidLoad() {
        super.viewDidLoad()

        if #available(iOS 9.0, *) {
            self.resultSearchController.loadViewIfNeeded()// iOS 9
        } else {
            // Fallback on earlier versions
            let _ = self.resultSearchController.view// iOS 8
        }
        self.resultSearchController = ({
            let controller = UISearchController(searchResultsController: nil)
            controller.searchResultsUpdater = self
            controller.dimsBackgroundDuringPresentation = false
            controller.searchBar.sizeToFit()
            controller.hidesNavigationBarDuringPresentation = false
            self.tableView.tableHeaderView = controller.searchBar
            return controller
        })()

The runtime warning (not sure if that's the issue here)

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<UISearchController: 0x7ff88130fdb0>)
Community
  • 1
  • 1
PoolHallJunkie
  • 1,345
  • 14
  • 33

2 Answers2

0

To remove the warning,Change the following code :

var resultSearchController = UISearchController()

to:

var resultSearchController : UISearchController!

And in viewWillDisappear method dismiss your resultSearchController.

Nata Mio
  • 2,168
  • 3
  • 22
  • 46
  • 1
    The first raises many errors in the code , it doesnt work. And I mentioned that i tried dismissing the searchBar , but it has a delay so it is still visible for 1-2 seconds before that. – PoolHallJunkie Nov 22 '15 at 19:44
  • 1
    @PoolHallJunkie, try dismissing it without animation or since your dismissing view controller remove it from superview,`removeFromSuperView()`. – Nata Mio Nov 23 '15 at 09:00
  • thx , its faster now. The warning is still there , UISearchController! is not working , only UISearchController!() but gets a fatal error – PoolHallJunkie Nov 23 '15 at 12:13
  • @PoolHallJunkie , what error are you getting ? nil optional ? just so you know that , you are not supposed to instantiate `UISearchController()` like that out side and as a class variable thats why your getting that warning, define it as` UISearchController! ` and instantiate it in `viewDidLoad` before using it so then you will not get fatal error or nil. – Nata Mio Nov 23 '15 at 12:22
  • I get the fatal error if I use UISearchController! , only UISearchController() works. with UISearchController I get "Expected member name or constructor call.." – PoolHallJunkie Nov 23 '15 at 14:10
  • the runtime warning is : "Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior () " – PoolHallJunkie Nov 23 '15 at 14:11
0

Thanks to UISearchController - Warning Attempting to load the view of a view controller

Try following:

class ViewController: UIViewController ,UISearchResultsUpdating {
var resultSearchController : UISearchController!

override func viewDidLoad() {
        super.viewDidLoad()


        self.resultSearchController = ({
            let controller = UISearchController()
            controller.searchResultsUpdater = self
            controller.dimsBackgroundDuringPresentation = false
            controller.searchBar.sizeToFit()
            controller.hidesNavigationBarDuringPresentation = false
            self.tableView.tableHeaderView = controller.searchBar
            return controller
        })()
    }
    deinit {
           self.searchController.loadViewIfNeeded()    // iOS 9
           let _ = self.searchController.view          // iOS 8
    }
Community
  • 1
  • 1
Rujoota Shah
  • 1,251
  • 16
  • 18