按下时提供相同细节的多个注释

Multiple annotations giving the same details when pressed

我有多个注释都正常工作并显示了我希望它们显示的所有内容。但是,我想在按下注释标注附件时显示详细视图。 我已经完成了所有工作,将细节传递到 calloutAccessoryControlTapped 函数中,这些函数仅在运行时最后一次加载,而不是按下注释。

我已经在互联网上搜索了这个问题的答案,但似乎找不到解决方案。

请帮忙!!

P.S。请原谅代码有些混乱,我一直在弄乱它,很多!!

class SecondViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

class CustomPointAnnotation: MKPointAnnotation {

    var imageName: String!
    var ID: String!

}


@IBOutlet weak var mapSeg: UISegmentedControl!
@IBOutlet weak var mainMapView: MKMapView!

@IBOutlet weak var webView: UIWebView!

var manager = CLLocationManager()
let signalsArray = ["MC 411 Signal", "MC 413 Signal ", "MC 414 Signal","MC 415 Signal", "MC 416 Signal", "MC 417 Signal", "MC 434 Signal", "MC 436 Signal", "MC 438 Signal"]
var segueIdentity: String?

override func viewDidLoad() {
    super.viewDidLoad()

    println("screen loaded")

    mainMapView.delegate = self


   loadUrl()



    var latitude:CLLocationDegrees = 55.691203
    var longitude:CLLocationDegrees = -3.668746

    var latDelta:CLLocationDegrees = 0.002  //zoom level, smaller = closer
    var lonDelta:CLLocationDegrees = 0.002


    var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
    var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
    var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
    var station = CustomPointAnnotation()

    mainMapView.mapType = MKMapType.Hybrid
    mainMapView.setRegion(region, animated: true)


    mainMapView.frame = CGRectMake(19, 72, 984, 321)
    webView.frame = CGRectMake(12, 397, 999, 321)


    //add a pin to the location
    station.coordinate = location
    station.title = "Carstairs Train Station"
    station.subtitle = "Motherwell Panel 6"
    station.imageName = "devil"
    station.ID = "station"
    mainMapView.addAnnotation(station)




    var sig1 = CustomPointAnnotation()
    var sig1Location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(55.690905, -3.66723)

    sig1.coordinate = sig1Location
    sig1.title = signalsArray [2]//MC414
    sig1.subtitle = "Caution, short signal section to MC 434"
    sig1.imageName = "GRS"
    sig1.ID = "sig1"

    mainMapView.addAnnotation(sig1)


    var sig2 = CustomPointAnnotation()
    var sig2Location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(55.688745, -3.662528)

    sig2.coordinate = sig2Location

    sig2.title = signalsArray [6]//MC434
    //sig2.subtitle = ""
    sig2.imageName = "devil"
    sig2.ID = "sig2"
    mainMapView.addAnnotation(sig2)



    var sig3 = CustomPointAnnotation()
    var sig3Location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(55.691119, -3.668915)

    sig3.coordinate = sig3Location

    sig3.title = signalsArray [3]//MC415
    //sig3.subtitle = ""
    sig3.imageName = "GRS"
    sig3.ID = "sig3"
    mainMapView.addAnnotation(sig3)



    var sig4 = CustomPointAnnotation()
    var sig4Location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(55.691087, -3.668950)

    sig4.coordinate = sig4Location

    sig4.title = signalsArray [5]//MC417
    //sig4.subtitle = ""
    sig4.imageName = "GRS"
    sig4.ID = "sig4"
    mainMapView.addAnnotation(sig4)



    var sig5 = CustomPointAnnotation()
    var sig5Location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(55.690856, -3.667783)

    sig5.coordinate = sig5Location

    sig5.title = signalsArray [4]//MC416
    //sig5.subtitle = ""
    sig5.imageName = "GRS"
    sig5.ID = "sig5"
    mainMapView.addAnnotation(sig5)



}

//Add a picture and button to annotation pin
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView!{

    if !(annotation is CustomPointAnnotation){

        return nil
    }

    let reuseId = "pin"

    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView

    if pinView == nil{

        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView!.canShowCallout = true
        pinView!.animatesDrop = true
        pinView!.pinColor = MKPinAnnotationColor.Purple

        pinView!.rightCalloutAccessoryView = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as UIButton


    }else {


    }

    let cpa = annotation as CustomPointAnnotation

    var imageview = UIImageView(frame: CGRectMake(0, 0, 45, 45))
    var segueId = cpa.ID

    pinView!.leftCalloutAccessoryView = imageview
    imageview.image = UIImage(named: cpa.imageName)

    println(segueId)
    segueIdentity = segueId

    return pinView
}


func mapView(mapView: MKMapView!, annotationView: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {


    if control == annotationView.rightCalloutAccessoryView {

        println("Right accessory pressed with " + segueIdentity!)


       pinSelected()


    }
}

func mapView(mapView: MKMapView!, didDeselectAnnotationView view: MKPointAnnotation) {


    println("selected annotation = " + segueIdentity!)
}



func pinSelected(){

    UIView .animateWithDuration(0.5, animations: {

        self.mainMapView.frame = CGRectMake(19, 72, 984, 595)
        self.webView.frame = CGRectMake(12, 670, 999, 50)

    })

    println("pinselected fired with " + (segueIdentity)!)

    self.performSegueWithIdentifier("pinSegue", sender: self)


}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {


    if (segue.identifier == "pinSegue"){

        println("prepare for segue fired! with " + (segueIdentity)!)

        /*CustomPointAnnotation annotationView = sender
        segue.destinationViewController setAnnotation*/

    }
}
  1. 您的许多委托方法正在引用您在 viewForAnnotation 中设置的 segueIdentity。您不应引用这些 class 属性来了解点击了哪个注释。

    您应该通过获取传递给您的注释视图来确定适当的注释,查看 annotation 属性,然后访问自定义注释的属性(例如 ID 属性) 在那个时候。

    func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
        if let annotation = view.annotation as? CustomPointAnnotation {
            let identifier = annotation.ID
    
            // now use `identifier` here, not `segueIdentity`
        }
    }
    

    坦率地说,我倾向于完全淘汰 segueIdentity 属性。

  2. 你的if pinView == nil需要和else子句,否则回收的注解视图会继续引用旧的注解,例如

    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView!.canShowCallout = true
        pinView!.animatesDrop = true
        pinView!.pinColor = MKPinAnnotationColor.Purple
    
        pinView!.rightCalloutAccessoryView = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as UIButton
    } else {
        pinView!.annotation = annotation
    }