9

When I'm in the middle of a search and then switch UItabs, ViewWillDisappear does not get called. Any idea as to why ViewWillDisappear does not get called when I have filtered results displaying and switch tabs?

func updateSearchResultsForSearchController(searchController: UISearchController) {
  if self.searchController?.searchBar.text.lengthOfBytesUsingEncoding(NSUTF32StringEncoding) > 0 {
        if let results = self.results {
            results.removeAllObjects()
        } else {
            results = NSMutableArray(capacity: MyVariables.dictionary.keys.array.count)
        }

        let searchBarText = self.searchController!.searchBar.text

        let predicate = NSPredicate(block: { (city: AnyObject!, b: [NSObject : AnyObject]!) -> Bool in
            var range: NSRange = NSMakeRange(0, 0)
            if city is NSString {

                range = city.rangeOfString(searchBarText, options: NSStringCompareOptions.CaseInsensitiveSearch)
            }

            return range.location != NSNotFound
        })

        // Get results from predicate and add them to the appropriate array.
        let filteredArray = (MyVariables.dictionary.keys.array as NSArray).filteredArrayUsingPredicate(predicate)
        self.results?.addObjectsFromArray(filteredArray)


        // Reload a table with results.
        self.searchResultsController?.tableView.reloadData()
    }
}




override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(self.identifier) as! UITableViewCell

    var text: String?
    var imgtext:AnyObject?
    if tableView == self.searchResultsController?.tableView {
        if let results = self.results {
            text = self.results!.objectAtIndex(indexPath.row) as? String
           imgtext = MyVariables.dictionary[text!]
            let decodedData = NSData(base64EncodedString: imgtext! as! String, options: NSDataBase64DecodingOptions(rawValue: 0) )
            var decodedimage = UIImage(data: decodedData!)


            cell.imageView?.image = decodedimage
        }
    } else {
        text = MyVariables.dictionary.keys.array[indexPath.row] as String
    }

    cell.textLabel!.text = text


    return cell
}

On the Load

  override func viewDidLoad() {
    super.viewDidLoad()

    let resultsTableView = UITableView(frame: self.tableView.frame)
    self.searchResultsController = UITableViewController()
    self.searchResultsController?.tableView = resultsTableView
    self.searchResultsController?.tableView.dataSource = self
    self.searchResultsController?.tableView.delegate = self

    // Register cell class for the identifier.
    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: self.identifier)
    self.searchResultsController?.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: self.identifier)

    self.searchController = UISearchController(searchResultsController: self.searchResultsController!)
    self.searchController?.searchResultsUpdater = self
    self.searchController?.delegate = self
    self.searchController?.searchBar.sizeToFit()
    self.searchController?.hidesNavigationBarDuringPresentation = false;
    self.tableView.tableHeaderView = self.searchController?.searchBar
    self.definesPresentationContext = true

}

3 Answers3

14

Had the same issue. viewWillDisappear is not called on the UITableViewController, but it is called in the UISearchController.

So I subclassed UISearchController and overrode the viewWillDisappear method. In my case I just needed to deactivate the search controller.

class SearchController: UISearchController {

    override func viewWillDisappear(_ animated: Bool) {        
        // to avoid black screen when switching tabs while searching
        isActive = false
    }
}
shim
  • 9,289
  • 12
  • 69
  • 108
Ivan Liljeqvist
  • 141
  • 1
  • 8
0

Similar to @user5130344 I found that subclassing resolved my issue, although I found that isActive = false cleared the search bar where I wanted the search query to remain on returning to the view.

Here's my subclass instead - this fixed my issue with iOS 13 dismissing the parent view:

class MySearchController: UISearchController {

    override func viewWillDisappear(_ animated: Bool) {
        // to avoid black screen when switching tabs while searching
        self.dismiss(animated: true)
    }
}
TahoeWolverine
  • 1,749
  • 2
  • 23
  • 31
-6

I think its the problem with the xcode . Try to close it and reopen the project once again and try to run again

sriram hegde
  • 2,301
  • 5
  • 29
  • 43
  • 1
    It's not xcode, I've recompiled it several times and closed xcode and reopened it dozens of times... There is something in my code that is not allowing the page to be released and I can't figure out what it is –  Jul 27 '15 at 15:47
  • try commenting out the code you feel a little suspicious (eg: code related to search ) and then try to navigate between tabs and see the problem persists cuz Im not able to find the problem – sriram hegde Jul 27 '15 at 15:59