How do I dismiss a UISearchController view?

核能气质少年 提交于 2019-12-25 04:55:12

问题


Context: There's a map view with a UISearchController implemented. The search controller displays its results in a different table view controller

I found this answer which explains how to dismiss the search controller within the same view. However, my results page is a different view than its origin page. I have

let resultsPage = ResultsTableViewController()
let searchController = UISearchController(searchResultsController: resultsPage)

So now when the search controller gets results, it displays them on the table view controller. When the user selects one of the results, I want to dismiss this view and go back to the map view, passing their selection along. Should I do this with a segue, or is there a better programmable approach?


回答1:


Did this same thing in my app. My map view controller has a search bar. When the user enters a search string, it presents a table view controller with a list of addresses that were found. The user can select an address which then dismisses the table view controller and drops a pin on the map for the selected address.

In the table view that displays, use didSelectRowAtIndexPath to dismiss the table view controller and use a protocol/delegate to pass the selected item back to the map view controller. Have your map view controller conform to this protocol. I did this exact setup and it works well.

protocol DropPinOnSelectedLocationDelegate {
  func dropPinOnLocation(placemark:MKPlacemark)
}

class LocationSearchTable: UITableViewController {
...

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath)
    self.mySearchController.searchBar.endEditing(true)
    if let indexPath = tableView.indexPathForCell(cell!) {
      selectedItem = matchingItems[indexPath.row].placemark
    }
    self.dismissViewControllerAnimated(true, completion: {
      self.delegate?.dropPinOnLocation(self.selectedItem!)
    })
  }
}


来源:https://stackoverflow.com/questions/38726205/how-do-i-dismiss-a-uisearchcontroller-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!