Swift 2.0:当有多个注释时,如何指定按下哪个注释?市场

时间:2016-01-20 12:04:19

标签: ios swift uibutton mapkit mapkitannotation

我确实遇到了一个问题,我不知道如何指定按下哪个UIButton .DetailDisclosure类型。我有多个注释,我想显示这个特定注释的细节。我希望这个问题很明确,有人可以帮助我。

提前谢谢。

这是我现在的代码

import UIKit
import MapKit
import CoreLocation

class FirstViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

var myURL = NSURL(string: "http://www...");
var Daten = NSMutableArray()
var Test = ""

@IBOutlet weak var Mapkit: MKMapView!

let locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    let request = NSMutableURLRequest(URL: myURL!)
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    _ = NSURLSession.sharedSession().downloadTaskWithRequest(request)


    let Session = NSURLSession.sharedSession()
    Session.dataTaskWithRequest(request)

            do {
                 let data = NSData(contentsOfURL: myURL!)
                _ = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)
                self.Daten = try (NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as! NSDictionary)["Ort"] as! NSMutableArray

                for parseJSON in self.Daten{

                    let resultName:String = parseJSON["Name"] as! String!;
                    let länge:String = parseJSON["breite"] as! String!;
                    let breite:String = parseJSON["laenge"] as! String!;

                    self.Test = resultName


                }
                        for Pin in self.Daten{
                            let Name = Pin["Name"] as! String!
                            let longitude = Pin["laenge"] as! String!
                            let breite = Pin["breite"] as! String!

                            let longitudeDouble = Double(longitude!)
                            let breitedouble = Double(breite!)

                            let Location = MKPointAnnotation()
                            let Coordinat = CLLocationCoordinate2DMake(longitudeDouble!, breitedouble!)

                            Location.coordinate = Coordinat
                            Location.title = Name

                            self.Mapkit.addAnnotation(Location)

                }


            }
            catch {
                print(error)
            }
 // more here
     }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

var anView:MKAnnotationView = MKAnnotationView()

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if (annotation is MKUserLocation) {
        //if annotation is not an MKPointAnnotation (eg. MKUserLocation),
        //return nil so map draws default view for it (eg. blue dot)...
        return nil
    }

    let reuseId = "test"

        if let annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) {
            annotationView.annotation = annotation
            return annotationView
        } else {
            let annotationView = MKPinAnnotationView(annotation:annotation, reuseIdentifier:reuseId)
            annotationView.enabled = true
            annotationView.canShowCallout = true


            let btn = UIButton(type: .DetailDisclosure)
            annotationView.rightCalloutAccessoryView = btn
            return annotationView
        }
}

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    //I don't know how to convert this if condition to swift 1.2 but you can remove it since you don't have any other button in the annotation view
    if (control as? UIButton)?.buttonType == UIButtonType.DetailDisclosure {
        mapView.deselectAnnotation(view.annotation, animated: false)
        performSegueWithIdentifier("Infos", sender: view)
    }
}

}

1 个答案:

答案 0 :(得分:0)

MKAnnotationfunc mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl)传递给您。您可以使用它在哈希映射中搜索正确的操作。

甚至更简单,如果只有一个注释不同,则测试相等性。