0

我确实有一个问题,我不知道如何指定按下哪个 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)
    }
}

}

4

1 回答 1

0

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

或者更简单,如果只有一个注释不同,则测试是否相等。

于 2016-01-20T12:13:47.957 回答