【发布时间】:2016-06-01 11:43:22
【问题描述】:
我正在开发一个 iOS 应用程序,但有一个小错误。我的项目包含以下内容:
- mapView 顶部的自定义搜索栏 (
HomeViewController.swift) - 使用显示搜索结果的表格视图查看 (
LocationSearchTable.swift)
搜索时,我将带有 tableview 的视图添加到我的 mapview 顶部和我的搜索栏后面。这一切都很好。
但是现在当我从 tableView 中选择一个搜索结果时,它会崩溃,因为它说我的 mapView 是 nil。
我在HomeViewController.swift 中有以下内容:
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
mapView.delegate = self
}
像这样将 LocationSearchTable 添加到 mapView:
func didChangeSearchText(searchText: String) {
if (searchText == "" ) {
self.locationSearchTable.view.removeFromSuperview()
} else {
self.view.insertSubview(self.locationSearchTable.view, aboveSubview: mapView)
}
}
在 LocationSearchTable.swift 上,我想在点击列表中的某些内容时调用 mapView:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let mapView = HomeViewController().mapView
print(mapView)
}
当尝试从那里访问 mapView 时,它是 nil。这是为什么?我在这里错过了什么?
亲切的问候,
沃特
【问题讨论】:
标签: ios swift uitableview mkmapview