UISearchController保留问题

我正在尝试使用UISearchController,但是我遇到了我无法解决的保留问题。 MainTableview有两个部分。

第一节

基于一些正则expression式的过滤数据

第二节

所有数据

我添加了UISearchController到我的tableview,并把ResultsTableController作为resultsTableController。 它工作时,用户search的东西,ResultsTableController前进,因为我设置tableview委托自我,从ResultsTableController中select项目调用didToolViewController中的didSelectRowAtIndexPath。 不过,如果用户从resultsTableController中select一些东西,我有分配问题。

以下情况发生在不同的场景

  • 用户不search任何东西,只要从MainTableview中select一个项目,我看到deinit消息
  • 用户search的东西,取消search,从MainTableviewselect项目,我看到deinit消息
  • 用户search的东西,并从ResultsTableController中select一个项目,我不看我的viewcontrollers

MainTableViewController.swift

var searchController: UISearchController! // Secondary search results table view. var resultsTableController: ResultsTableController! var allCompanies = ["Data1","Data2","Data3"] override func viewDidLoad() { super.viewDidLoad() resultsTableController = ResultsTableController() // We want to be the delegate for our filtered table so didSelectRowAtIndexPath(_:) is called for both tables. resultsTableController.tableView.delegate = self searchController = UISearchController(searchResultsController: resultsTableController) searchController.searchResultsUpdater = self searchController.searchBar.sizeToFit() tableView.tableHeaderView = searchController.searchBar searchController.delegate = self searchController.dimsBackgroundDuringPresentation = false searchController.searchBar.delegate = self definesPresentationContext = true } } // MARK: UISearchBarDelegate func searchBarSearchButtonClicked(searchBar: UISearchBar) { searchBar.resignFirstResponder() } // MARK: UISearchResultsUpdating func updateSearchResultsForSearchController(searchController: UISearchController) { // Update the filtered array based on the search text. let filteredResults = allCompanies.filter({ company in (company.lowercaseString as NSString).containsString(searchController.searchBar.text.lowercaseString) }) // Hand over the filtered results to our search results table. let resultsController = searchController.searchResultsController as! ResultsTableController resultsController.searchResult = filteredResults resultsController.tableView.reloadData() } // usual tableview methods override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if resultsTableController.searchResult.count > 0 { selectedCompany = resultsTableController.searchResult[index] //do something with selected company navigationController?.popViewControllerAnimated(true) return } // selectedCompany = allCompanies[index] navigationController?.popViewControllerAnimated(true) } deinit { println("MainTableView deinit") } 

ResultTableController.swift

 class ResultsTableController:UITableViewController { var searchResult = [String]() override func viewDidLoad() { super.viewDidLoad() tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return searchResult.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell let index = indexPath.row cell.textLabel?.font = UIFont(name: "Avenir-Roman", size: 16) cell.textLabel?.text = searchResult[index].description return cell } deinit { println("ResultTableController deinit") } } 

嘿,我今天遇到了这个问题,显然我需要强制closuressearchController解决保留问题

  override func viewWillDisappear(animated: Bool) { super.viewWillDisappear(animated) searchController?.dismissViewControllerAnimated(false, completion: nil) } 

这里是我的示例项目https://www.dropbox.com/s/zzs0m4n9maxd2u5/TestSearch.zip?dl=0

该解决scheme似乎是在某个时候在UISearchController上调用dismissViewControllerAnimated。 大多数人可能不这样做,因为UISearchController是一些与你的视图控制器托pipeUISearchController有关的实现细节。

我的解决scheme,似乎无论如何显示您的search用户界面(标准目前或在popup窗口)如何工作是调用searchController.dismissViewControllerAnimated()从您的主机的viewDidDisappear,检查后,看看是否不再呈现视图控制器。 这可以捕获所有的情况,特别是用户在popup窗口之外轻按以自动closures用户界面的情况,或者search用户界面正在消失的情况,因为您已将其他内容推送到导航堆栈中。 在后一种情况下,你不想解雇UISearchController。

 override func viewDidDisappear(animated: Bool) { super.viewDidDisappear(animated) if presentingViewController == nil { searchController.dismissViewControllerAnimated(false, completion: nil) } }