Swift Annotation CallOut按钮选择

Fab*_*gue 1 mkmapview mkannotation mkannotationview ios swift

我有一个mapView自定义注释.我还添加了一个按钮,但有没有办法看到按下按钮的注释?

因此,控制台的帖子不仅应该是"Disclosure Pressed!".我需要像"Disclosure Pressed!info(X) .subtitle"这样的内容来查看用户录制的info1或info2.

这是代码:

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet weak var Map: MKMapView!

    class CustomPointAnnotation: MKPointAnnotation {
        var imageName: String!
    }

    @IBAction func btpressed(sender: AnyObject) {

    }

    override func viewDidLoad() {
        super.viewDidLoad()
        //1
        var lat1:CLLocationDegrees = 40.748708
        var long1:CLLocationDegrees = -73.985643
        var latDelta1:CLLocationDegrees = 0.01
        var longDelta1:CLLocationDegrees = 0.01

        var span1:MKCoordinateSpan = MKCoordinateSpanMake(latDelta1, longDelta1)
        var location1:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat1, long1)
        var region1:MKCoordinateRegion = MKCoordinateRegionMake(location1, span1)

        Map.setRegion(region1, animated: true)

        var info1 = CustomPointAnnotation()
        info1.coordinate = location1
        info1.title = "Test Title1!"
        info1.subtitle = "Subtitle1"
        info1.imageName = "1.png"

        Map.addAnnotation(info1)

        //2
        var lat2:CLLocationDegrees = 41.748708
        var long2:CLLocationDegrees = -72.985643
        var latDelta2:CLLocationDegrees = 0.01
        var longDelta2:CLLocationDegrees = 0.01

        var span2:MKCoordinateSpan = MKCoordinateSpanMake(latDelta2, longDelta2)
        var location2:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat2, long2)
        var region2:MKCoordinateRegion = MKCoordinateRegionMake(location2, span2)

        var info2 = CustomPointAnnotation()
        info2.coordinate = location2
        info2.title = "Test Title2!"
        info2.subtitle = "Subtitle2"
        info2.imageName = "2.png"
        Map.addAnnotation(info2)
    }

    func mapView(mapView: MKMapView!, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

        if control == annotationView.rightCalloutAccessoryView {
            println("Disclosure Pressed!")
        }
    }

    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
        if !(annotation is CustomPointAnnotation) {
            return nil
        }

        let reuseId = "test"

        var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
        if anView == nil {
            anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            anView.canShowCallout = true

            anView.rightCalloutAccessoryView = UIButton.buttonWithType(.InfoDark) as UIButton

            var imageview = UIImageView(frame: CGRectMake(0, 0, 10, 10))
            imageview.image = UIImage(named: "1.png")
            anView!.leftCalloutAccessoryView = imageview
        } else {
            anView.annotation = annotation
        }

        //Set annotation-specific properties **AFTER**
        //the view is dequeued or created...

        let cpa = annotation as CustomPointAnnotation
        anView.image = UIImage(named:cpa.imageName)

        return anView
    }

}
Run Code Online (Sandbox Code Playgroud)

我已经尝试过类似的东西:

if control == annotationView.rightCalloutAccessoryView {
    println("Disclosure Pressed!" \(self.subtitle))
}
Run Code Online (Sandbox Code Playgroud)

小智 6

calloutAccessoryControlTapped委托方法中,self不是其标注其标注附件视图的注释.

self指的是包含方法的类的实例(即实例ViewController).另一个问题是,在中println,您将变量引用放在引号之外而不是在内部.


委托方法为您提供了对注释视图的引用:annotationView参数.

注释视图包含对注释的引用:annotationView.annotation.


但请注意,您的委托方法声明与记录的方法声明略有不同(annotationView参数具有本地名称view,其他参数标记为带!后缀的可选项).

尽管您的版本在技术上仍然有用,但最好使用文档化的声明.

下面的完整示例还说明了如何检查注释是否是您的自定义对象之一以及如何访问自定义属性:

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, 
             calloutAccessoryControlTapped control: UIControl!) {

    if control == view.rightCalloutAccessoryView {
        println("Disclosure Pressed! \(view.annotation.subtitle)")

        if let cpa = view.annotation as? CustomPointAnnotation {
            println("cpa.imageName = \(cpa.imageName)")
        }
    }

}
Run Code Online (Sandbox Code Playgroud)


旁注:
由于您已将其设置leftCalloutAccessoryView为a UIImageView,因此仅对其作为子类的视图调用时,不会调用calloutAccessoryControlTappedUIControl. UIImageView不是的子类UIControl.如果您需要检测左侧附件上的水龙头,请创建自定义UIButton(并设置其图像)而不是a UIImageView.