MortalMan
MortalMan

Reputation: 2612

Attempting to load the view of a view controller while it is deallocating... UISearchController

I have code that creates a UISearchController' in my UIVIew'sviewDidLoad`.

 self.resultSearchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.searchBar.delegate = self
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()
        controller.hidesNavigationBarDuringPresentation = false //prevent search bar from moving
        controller.searchBar.placeholder = "Search for song"

        self.myTableView.tableHeaderView = controller.searchBar

        return controller

    })()

Right after this closure finishes, this warning appears in the console:

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<UISearchController: 0x154d39700>)

I don't get what I am doing wrong. This similar question is not really my situation (At least I don't think so). What is going on?

Upvotes: 82

Views: 38294

Answers (13)

JJH
JJH

Reputation: 1201

UISearchController's view has to be removed from its superview before deallocate. (guess it is a bug)

Objective-C...

-(void)dealloc { 
    [searchController.view removeFromSuperview]; // It works!
}

Swift 3...

deinit {
    self.searchController.view.removeFromSuperview()
}

I struggled with this issue for a couple of weeks. ^^

Upvotes: 120

david
david

Reputation: 401

It seem the view is lazy loaded, if you allocated the controller and never show it, the view is not loaded. In this case, if the controller is deallocated, you will received this warning. you could show it once, or call it's loadViewIfNeed() method, or use 'let _ = controller.view' to force load the view to avoid this warning.

Upvotes: 1

Niels
Niels

Reputation: 299

Creating a search controller in viewDidLoad() and setting its search bar as the navigation item's title view doesn't create a strong reference to the search controller, which is why it's deallocated.

So instead of doing this:

override func viewDidLoad() {
    super.viewDidLoad()
    // Create search controller
    let searchController = UISearchController(searchResultsController: nil)
    // Add search bar to navigation bar
    navigationItem.titleView = searchController.searchBar
    // Size search bar
    searchController.searchBar.sizeToFit()
}

You should do this:

var searchController: UISearchController!

override func viewDidLoad() {
    super.viewDidLoad()
    // Create search controller
    searchController = UISearchController(searchResultsController: nil)
    // Add search bar to navigation bar
    navigationItem.titleView = searchController.searchBar
    // Size search bar
    searchController.searchBar.sizeToFit()
}

Upvotes: 2

mehmetsen80
mehmetsen80

Reputation: 737

Mine is working like this

func initSearchControl(){

        searchController = UISearchController(searchResultsController: nil)

        if #available(iOS 9.0, *) {
            searchController.loadViewIfNeeded()
        } else {
            let _ = self.searchController.view
        }

        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false
        definesPresentationContext = true
        tableView.tableHeaderView = searchController.searchBar
        searchController.searchBar.sizeToFit()
    }

searchController.loadViewIfNeeded() solves the problem but you need to call it after initializing the searchController

Upvotes: 2

harsh_v
harsh_v

Reputation: 3269

class SampleClass: UITableViewController, UISearchBarDelegate {

private let searchController =  UISearchController(searchResultsController: nil)

 override func viewDidLoad() {
        super.viewDidLoad()

        searchController.loadViewIfNeeded() // Add this line before accessing searchController
 }

}

Upvotes: 11

Milos Mandic
Milos Mandic

Reputation: 1071

In Swift 2.2 version that worked for me

deinit {
    self.searchController?.view.removeFromSuperview()
}

I think it's helpful!

Upvotes: 7

titusmagnus
titusmagnus

Reputation: 2082

I'm a bit late to the party, but here's my solution:

var resultSearchController: UISearchController!

override func viewDidLoad()
{
    super.viewDidLoad()

    self.resultSearchController = ({
        let searchController = UISearchController(searchResultsController: nil)
        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false
        searchController.searchBar.sizeToFit()
        return searchController
    })()

    self.tableView.tableHeaderView = self.resultSearchController.searchBar
    self.tableView.reloadData()
}

I hope it works for you.

Upvotes: 0

nijm
nijm

Reputation: 2218

Here is the Swift version that worked for me (similar toJJHs answer):

deinit{
    if let superView = resultSearchController.view.superview
    {
        superView.removeFromSuperview()
    }
}

Upvotes: 20

Nikolai Ischuk
Nikolai Ischuk

Reputation: 111

It's not a bug. It seems that you have to avoid creating ViewControllers without presenting them. So after SomeViewController() or let variable: SomeViewController you have to call something like this self.presentViewController(yourViewController ...etc). If you don't do that, you will get this warning when this view controller will be dealocated.

Upvotes: 2

Michael L. Mehr
Michael L. Mehr

Reputation: 51

I used Derek's answer, but had to change it slightly. The answer that was provided crashed for me because the call to loadViewIfNeeded() happened before the resultSearchController was defined. (My declaration was

var resultSearchController: UISearchController!

). So I just moved it afterwards and it worked.

If I left out the call entirely, the bug remained, so I'm sure it is an essential part of the answer. I was unable to test it on iOS 8.

Upvotes: 1

Vincent
Vincent

Reputation: 4409

In Swift2 I got the same error message due to an obvious bug:

let alertController = UIAlertController(title: "Oops",
    message:"bla.", preferredStyle: UIAlertControllerStyle.Alert)

alertController.addAction(UIAlertAction(title: "Ok", 
     style: UIAlertActionStyle.Default,handler: nil))

self.presentViewController(alertController, animated: true, completion: nil)

Due to a stupid copy error from myself, I had not included the self.presentViewController line. This caused the same error.

Upvotes: 7

Derek
Derek

Reputation: 2112

Hacking together a few solutions I managed to get mine working by adding lines to viewDidLoad before fully setting up the UISearchController:

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.rightBarButtonItem = self.editButtonItem()

    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()

        self.tableView.tableHeaderView = controller.searchBar

        return controller
    })()

    self.tableView.reloadData()

}

Upvotes: 10

MortalMan
MortalMan

Reputation: 2612

Solved! It was a simple fix. I changed this code

class ViewController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate {

    var resultSearchController = UISearchController()

to this:

 class ViewController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate {

    var resultSearchController: UISearchController!

This fixes the problem.

Upvotes: 36

Related Questions