ios - swift 2.0 : How do I specify which Annotation is pressed when having more then one Annotation? 市场

标签 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)
    }
}

}

最佳答案

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

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

关于ios - swift 2.0 : How do I specify which Annotation is pressed when having more then one Annotation? 市场,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34899571/

相关文章:

ios - 用户界面标签 : change a text color when click button

ios - 在 Swift 中检测 CAShapeLayer 上的点击?

ios - 在UICollectionViewCell中使用Button编辑难点

iphone - 是否有 UIView 调整大小事件?

ios - 如何使用NSJSONSerialization快速获取海量数据响应?

ios - 在 Swift 中使用 value 参数时 genstrings 会阻塞

swift - 使用 Xcode 捕获 Swift 代码中的崩溃

快速从云工具包中加载所有记录,避免限制结果

iphone - 通过 (id)sender 访问 UIButton

iphone - 使用单个方法调用设置 UIButton 的标题?