У меня возникла проблема с тем, что я не знаю, как указать тип UIButton .DetailDisclosure. У меня есть несколько аннотаций, и я хочу показать детали этой конкретной аннотации. Надеюсь, этот вопрос ясен, и кто-то может мне помочь.Swift 2.0: Как указать, какая аннотация нажата при наличии более одной аннотации? Рынок
Заранее спасибо.
Это мой код прямо сейчас
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)
}
}
}