【问题标题】:Value of type 'UIViewController' has no member 'mapView'; did you mean 'loadView'?“UIViewController”类型的值没有成员“mapView”;你是说'loadView'吗?
【发布时间】:2019-02-09 21:22:55
【问题描述】:

我尝试在 Swift4 中使用 MKLocalSearchRequest 搜索位置(适用于 ios 12)。当我在 viewDidLoad 的末尾附加locationSearchTable.mapView = mapView 时,我得到了错误。

在视图控制器中

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {


    @IBOutlet weak var mapView: MKMapView!

    let locationManager = CLLocationManager()
    var resultSearchController: UISearchController? = nil

    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()

        let locationSearchTable = storyboard!.instantiateViewController(withIdentifier: "LocationSearchTable")
        resultSearchController = UISearchController(searchResultsController: locationSearchTable)
        resultSearchController?.searchResultsUpdater = locationSearchTable as! UISearchResultsUpdating

        let searchBar = resultSearchController!.searchBar
        searchBar.sizeToFit()
        searchBar.placeholder = "Search for places"
        navigationItem.titleView = resultSearchController?.searchBar

        resultSearchController?.hidesNavigationBarDuringPresentation = false
        resultSearchController?.dimsBackgroundDuringPresentation = true
        definesPresentationContext = true

        locationSearchTable.mapView = mapView

    }...


在位置搜索表中

import UIKit
import MapKit


class LocationSearchTable: UITableViewController {

    var matchingItems: [MKMapItem] = []
    var mapView: MKMapView? = nil

}

extension LocationSearchTable: UISearchResultsUpdating {
    func updateSearchResults(for searchController: UISearchController) {
        guard let mapView = mapView,
            let searchBarText = searchController.searchBar.text else { return }
        let request = MKLocalSearch.Request()
        request.naturalLanguageQuery = searchBarText
        request.region = mapView.region
        let search = MKLocalSearch(request: request)
        search.start { response, _ in
            guard let response = response else {
                return
            }
            self.matchingItems = response.mapItems
            self.tableView.reloadData()
        }
    }
}...

【问题讨论】:

    标签: mkmapview mklocalsearch


    【解决方案1】:

    也许 locationSearchTable 不是LocationSearchTable 类。

    试试下面的代码

    if let locationSearchTable = locationSearchTable as? LocationSearchTable {
        locationSearchTable.mapView = mapView
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 2016-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多