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

前端 未结 13 1112
一生所求
一生所求 2020-12-01 02:34

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

 self.resultSearchController = ({
        let controller = UISearch         


        
相关标签:
13条回答
  • 2020-12-01 03:09

    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.

    0 讨论(0)
  • 2020-12-01 03:10

    In Swift 2.2 version that worked for me

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

    I think it's helpful!

    0 讨论(0)
  • 2020-12-01 03:14

    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.

    0 讨论(0)
  • 2020-12-01 03:15

    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()
    
    }
    
    0 讨论(0)
  • 2020-12-01 03:15

    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.

    0 讨论(0)
  • 2020-12-01 03:18
    class SampleClass: UITableViewController, UISearchBarDelegate {
    
    private let searchController =  UISearchController(searchResultsController: nil)
    
     override func viewDidLoad() {
            super.viewDidLoad()
    
            searchController.loadViewIfNeeded() // Add this line before accessing searchController
     }
    
    }
    
    0 讨论(0)
提交回复
热议问题