Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchController causes black screen Swift 2.0

I have a strange problem in iOS 9 with Swift 2.0. I added UISearchController in my tableViewController but it causes a strange black screen problem. When I press the search bar and write something it shows my filtered results without any problem but when I tap another tab bar button like Bookmarks and after that when I tap tableViewController which is Most Viewed again it shows black screen like screen shot.

There is my tableViewController;

import UIKit

class CitiesTableViewController: UITableViewController, UISearchResultsUpdating {

// MARK: - Class Properties

private var cities = [String]()
private var veterinaries = [Veterinary]()
private var filteredVeterinaries = [Veterinary]()
private var resultSearchController: UISearchController!

// MARK: - TableViewController Life Cycle Methods

override func viewDidLoad() {
    super.viewDidLoad()
    self.getCitiesList()
    self.configureResultsSearchController()
}

override func viewDidDisappear(animated: Bool) {
    super.viewDidDisappear(animated)
    self.resultSearchController.active = false
}

// MARK: - Configuring Search Bar Controller

private func configureResultsSearchController() {
    self.resultSearchController = UISearchController(searchResultsController: nil)
    self.resultSearchController.searchResultsUpdater = self
    self.resultSearchController.dimsBackgroundDuringPresentation = false
    self.resultSearchController.hidesNavigationBarDuringPresentation = false
    self.resultSearchController.searchBar.sizeToFit()
    self.resultSearchController.searchBar.placeholder = "Klinik veya ilçe adı"
    self.tableView.tableHeaderView = self.resultSearchController.searchBar
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 }
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if self.resultSearchController.active { return self.filteredVeterinaries.count }
    else { return self.cities.count }
}

// MARK: - Table view Delegate Methods

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if (self.resultSearchController.active) {
        self.performSegueWithIdentifier(Constants.ShowDetailViewControllerSegueIdentifier, sender: nil)
    } else {
        self.performSegueWithIdentifier(Constants.ShowTownsTableViewControllerSegueIdentifier, sender: nil)
    }
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(Constants.CellIdentifier, forIndexPath: indexPath)
    if (self.resultSearchController.active) {
        cell.textLabel?.text = self.filteredVeterinaries[indexPath.row].name
        cell.detailTextLabel?.text = self.filteredVeterinaries[indexPath.row].address
        return cell
    } else {
        cell.textLabel?.text = self.cities[indexPath.row]
        return cell
    }
}

// MARK: - PARSE Query Methods

private func getCitiesList() {
    let parseQueries = ParseQueries()
    parseQueries.downloadListData() {
        (let parseResults) in
        if let veterinaries = parseResults as? [Veterinary] {
            self.veterinaries = veterinaries
            for vet in veterinaries {
                if let city = vet.city {
                    self.cities.append(city)
                }
            }
            dispatch_async(dispatch_get_main_queue()) {
                self.cities = HelperMethods().removeDuplicatesAndSort(array: self.cities)
                self.tableView.reloadData()
            }
        }
    }
}

// MARK: - UISearchController Delegate Methods

func updateSearchResultsForSearchController(searchController: UISearchController) {
    self.filteredVeterinaries.removeAll(keepCapacity: false)
    if let searchBarText = searchController.searchBar.text{
        let searchText = searchBarText.lowercaseString
        // Searching with Veterinary Name and Veterinary City
        self.filteredVeterinaries = self.veterinaries.filter({$0.name?.lowercaseString.rangeOfString(searchText) != nil})
        self.filteredVeterinaries += self.veterinaries.filter({$0.town?.lowercaseString.rangeOfString(searchText) != nil})
        tableView.reloadData()
    }
}

This is the black screen image from iOS 9 simulator same as real device. enter image description here

I think its deiniting my tableView when I tap the searchBar and it can't init again. Is this a bug or something ?

How can I solve this problem ?

Thank you !

like image 330
emresancaktar Avatar asked Oct 22 '15 21:10

emresancaktar


1 Answers

Friend, in your viewDidLoad() insert this code line:

self.definesPresentationContext = true

See how I put (line 29):

click here to see

like image 188
Jonathan Ribeiro Avatar answered Nov 04 '22 13:11

Jonathan Ribeiro