首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >快速注释CallOut按钮选择

快速注释CallOut按钮选择
EN

Stack Overflow用户
提问于 2014-09-03 12:32:17
回答 1查看 9.9K关注 0票数 1

我有一个带有自定义注释的mapView。我还添加了一个按钮,但是有什么方法可以查看哪个注释是按下的吗?

所以到控制台的帖子不应该只是“公开按下!”。我需要像“公开按下!信息(X).subtitle”这样的东西来看到用户录制的info1或info2。

以下是代码:

代码语言:javascript
复制
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
    }

}

我已经尝试过这样的方法:

代码语言:javascript
复制
if control == annotationView.rightCalloutAccessoryView {
    println("Disclosure Pressed!" \(self.subtitle))
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-04 21:02:29

calloutAccessoryControlTapped委托方法中,self不是其标注附件视图被点击的注释。

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

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

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

但是,请注意您的委托方法声明与文档中的方法声明略有不同(annotationView参数有一个本地名称view,其他参数被标记为可选的!后缀)。

最好使用文档化的声明,即使您的版本在技术上仍然有效。

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

代码语言:javascript
复制
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)")
        }
    }

}

附带说明:

既然您已经将leftCalloutAccessoryView设置为UIImageView,那么点击它就不会调用calloutAccessoryControlTapped,因为它只调用作为UIControl子类的视图。UIImageView不是UIControl的子类。如果您需要检测左侧附件上的点击,请创建一个自定义UIButton (并设置其图像),而不是UIImageView

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25644421

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档