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

前端 未结 13 1110
一生所求
一生所求 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 02:54

    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. ^^

    0 讨论(0)
  • 2020-12-01 02:54

    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.

    0 讨论(0)
  • 2020-12-01 02:59

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

    deinit{
        if let superView = resultSearchController.view.superview
        {
            superView.removeFromSuperview()
        }
    }
    
    0 讨论(0)
  • 2020-12-01 03:03

    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.

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

    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

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

    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.

    0 讨论(0)
提交回复
热议问题