gpt4 book ai didi

ios - 从注释中单击按钮发送对象 swift 4

转载 作者:行者123 更新时间:2023-11-30 11:44:07 25 4
gpt4 key购买 nike

我继承了 UIbutton 中的注释类,例如:

 class MapButton : UIButton{
var clickedAnnotation : annotations? = nil
}

现在,我尝试读取注释中每个注释的属性,如下所示:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}

let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)

let colorPointAnnotation = annotation as! annotations
pinView?.pinTintColor = colorPointAnnotation.pinColor

let pinIsCurrentLocation = colorPointAnnotation.subtitle
if pinIsCurrentLocation != "Current"
{
pinView?.canShowCallout = true
}
let smallSquare = CGSize(width: 30, height: 30)
let button = MapButton(frame: CGRect(origin: CGPoint.zero, size: smallSquare))
button.setBackgroundImage(UIImage(named: "tasks"), for: .normal)
button.clickedAnnotation = colorPointAnnotation
button.addTarget(self, action: #selector(MapController.TaskDetail(_:)), for: .touchUpInside)
pinView?.leftCalloutAccessoryView = button
}
else {
pinView?.annotation = annotation
}

return pinView
}

但是我在这一行中收到错误“Type 'MapController' has no member 'TaskDetail'”,并且无法读取注释中的属性。

button.addTarget(self, action: #selector(MapController.TaskDetail(_:)), for: .touchUpInside)

按钮点击方法

func TaskDetail(_sender: MapButton){
// Want to read the properties of each annotation when clicked on the map.
}

最佳答案

如果您使用的是 Swift 4 并且已在其中声明,请确保在 func TaskDetail(_sender: MapButton) 之前添加 @objcMapController

例如

class MapController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}

let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)

let colorPointAnnotation = annotation as! annotations
pinView?.pinTintColor = colorPointAnnotation.pinColor

let pinIsCurrentLocation = (colorPointAnnotation?.subtitle)!
if pinIsCurrentLocation != "Current"
{
pinView?.canShowCallout = true
}
let smallSquare = CGSize(width: 30, height: 30)
let button = MapButton(frame: CGRect(origin: CGPoint.zero, size: smallSquare))
button.setBackgroundImage(UIImage(named: "tasks"), for: .normal)
button.clickedAnnotation = colorPointAnnotation
button.addTarget(self, action: #selector(TaskDetail(_sender:)), for: .touchUpInside)
pinView?.leftCalloutAccessoryView = button
}
else {
pinView?.annotation = annotation
}

return pinView
}

@objc func TaskDetail(_sender: MapButton){
// Want to read the properties of each annotation when clicked on the map.
}
}

还有你的MapButton

class MapButton : UIButton{
var clickedAnnotation : annotations? = nil
}

希望这会有所帮助!

关于ios - 从注释中单击按钮发送对象 swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49040539/

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