如何关闭 UISearchController 视图? [英] How do I dismiss a UISearchController view?

查看:34
本文介绍了如何关闭 UISearchController 视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:有一个实现了 UISearchController 的地图视图.搜索控制器在不同的表视图控制器中显示其结果

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)

所以现在当搜索控制器获得结果时,它会在表视图控制器上显示它们.当用户选择其中一个结果时,我想关闭此视图并返回到地图视图,同时传递他们的选择.我应该用 segue 来做到这一点,还是有更好的可编程方法?

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?

推荐答案

在我的应用中做了同样的事情.我的地图视图控制器有一个搜索栏.当用户输入搜索字符串时,它会显示一个带有找到的地址列表的表视图控制器.用户可以选择一个地址,然后关闭表视图控制器并在地图上为所选地址放置一个图钉.

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.

在显示的表格视图中,使用 didSelectRowAtIndexPath 关闭表格视图控制器并使用协议/委托将所选项目传回地图视图控制器.让您的地图视图控制器符合此协议.我做了这个确切的设置,效果很好.

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!)
    })
  }
}

这篇关于如何关闭 UISearchController 视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆