どの 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)
}
}
}