簡體   English   中英

即使設置了委托,MKMapview 委托也從未被調用

[英]MKMapview delegate never called even with set delegate

我有一個集成滾動視圖的 UIView。 在滾動視圖中,我有一張地圖,用戶需要在其中移動圖釘。 稍后我將重用 pin 的位置。

我正在為地圖設置強引用和設置委托(設計和代碼)。 我能夠看到創建的圖釘,但有自定義圖標和將其設置為可拖動的可能性,我已經實現了 mapView( viewFor MKAnnotation

這似乎從未被調用過,我不明白為什么。

我還嘗試讓另一個委托作為 didfinishloading 來了解問題是否與 mkannotation 相關,但也從未調用過這個委托。

import Foundation
import Firebase
import MapKit
import UIKit


public class Setup: UIViewController,CLLocationManagerDelegate,MKMapViewDelegate{


    var ChargePointID = ""
    @IBOutlet weak var chargepointIDLabel: UILabel!


    let locationManager = CLLocationManager()
    var pin: customPin!
    //setting up variables
    //pricing textfield and slider
    @IBOutlet weak var priceLabel: UITextField!
    @IBOutlet weak var priceSlider: UISlider!


    @IBOutlet var map: MKMapView!

    override public func viewDidLoad() {
        super.viewDidLoad()
         chargepointIDLabel.text = ChargePointID
        self.map.delegate = self
        self.map.mapType = .hybrid
        // Ask for Authorisation from the User.
        self.locationManager.requestAlwaysAuthorization()

        // For use in foreground
        self.locationManager.requestWhenInUseAuthorization()

        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            locationManager.startUpdatingLocation()
        }

        //create liocation relative to user
        let location = CLLocationCoordinate2D(latitude: (locationManager.location?.coordinate.latitude)!, longitude: (locationManager.location?.coordinate.longitude)!)

        //centering map to user location
        let region = MKCoordinateRegion(center: location, span: MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005))
        self.map.setRegion(region, animated: true)

        //creating a pin of type custompin
        pin = customPin(pinTitle: ChargePointID, pinSubTitle: "", location: location)

        map.addAnnotation(pin)


    }

    private func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
        print("locations = \(locValue.latitude) \(locValue.longitude)")
    }



    private func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        print("annotation title == \(String(describing: view.annotation?.title!))")
    }


    private func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

        print("ok")
        let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "customannotation")
        annotationView.image = UIImage(named:"setuppin")
        annotationView.canShowCallout = true
        return annotationView

    }

    func mapViewDidFinishLoadingMap(_ mapView: MKMapView) {
print(" map has finished")
    }


    private func map(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

        print("ok")
        let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "customannotation")
        annotationView.image = UIImage(named:"setuppin")
        annotationView.canShowCallout = true
        return annotationView

    }







}

class customPin: NSObject, MKAnnotation {
    var coordinate: CLLocationCoordinate2D
    var title: String?
    var subtitle: String?

    init(pinTitle:String, pinSubTitle:String, location:CLLocationCoordinate2D) {
        self.title = pinTitle
        self.subtitle = pinSubTitle
        self.coordinate = location
    }
}

private是沒有必要的。 代替

private func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

這是因為 Xcode 引發了這個警告

實例方法 'mapView( :viewFor:)' 幾乎匹配協議 'MKMapViewDelegate' 的可選要求 'mapView( :viewFor:)'

並建議將其設為私有作為修復。 順便說一句,即使沒有私人也不會提出任何問題

該死!!!!!

我解決了它......警告是因為我的班級被宣布為公開廣告,委托也需要如此。

我從班級中刪除了公開聲明,一切都開始起作用了。

所以最終的問題是主類被聲明為公開的!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM