gpt4 book ai didi

ios - Swift Annotation CallOut 按钮选择

转载 作者:搜寻专家 更新时间:2023-11-01 05:59:29 25 4
gpt4 key购买 nike

我有一个带有自定义注释的 mapView。我还为此添加了一个按钮,但有没有办法查看按下按钮的注释?

因此,控制台的帖子不应该只是“披露压力!”。我需要类似“披露压力!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
}

}

我已经试过了:

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

最佳答案

calloutAccessoryControlTapped委托(delegate)方法中,self不是callout accessory view被点击的注解。

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


委托(delegate)方法为您提供对注释 View 的引用:annotationView 参数。

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


但是,请注意您的委托(delegate)方法声明与文档中的声明略有不同(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)")
}
}

}


旁注:
由于您已将 leftCalloutAccessoryView 设置为 UIImageView,因此点击它不会调用 calloutAccessoryControlTapped,因为它只是调用作为 UIControl 子类的 View 。 UIImageView 不是 UIControl 的子类。如果您需要检测左侧配件上的点击,请创建自定义 UIButton(并设置其图像)而不是 UIImageView

关于ios - Swift Annotation CallOut 按钮选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25644421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com