gpt4 book ai didi

ios - 自定义注释内的按钮不会使用react

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

我设计了一个自定义注释, View 看起来很好。但是 View 中有一个按钮,当我点击它时,什么也没有发生。它应该首先在日志中打印一些内容。这是我的自定义注释类代码。

import UIKit
import MapKit
import CoreLocation
import CoreData


class CustomAnnotation: MKPinAnnotationView, MKMapViewDelegate, CLLocationManagerDelegate {

let firmaLabel : UILabel = UILabel.init(frame:CGRect(x: 0, y: -50, width: 200, height: 50))
let infoButton = UIButton(type: .detailDisclosure)


override func setSelected(_ selected: Bool, animated: Bool)
{
super.setSelected(true, animated: animated)


firmaLabel.textAlignment = .center
firmaLabel.textColor = UIColor.white
firmaLabel.font = UIFont.init(name: "Trebuchet MS", size: 20)
firmaLabel.backgroundColor = UIColor.black
firmaLabel.layer.borderColor = UIColor.darkGray.cgColor
firmaLabel.layer.cornerRadius = 5
firmaLabel.layer.masksToBounds = true

infoButton.frame = CGRect(x: -50, y: -50, width: 50, height: 50)
infoButton.backgroundColor = UIColor.red
infoButton.layer.borderColor = UIColor.darkGray.cgColor
infoButton.layer.cornerRadius = 5
infoButton.layer.masksToBounds = true
infoButton.setTitle(">", for: UIControlState.normal)

//The code below is the way I imagine it should make it work??

infoButton.addTarget(self, action: #selector(btnClicked(_:)), for: UIControlEvents.touchUpInside)

firmaLabel.text = "Hello"
self.addSubview(firmaLabel)

self.addSubview(infoButton)

}

func btnClicked (_: UIButton) {
print("Hello world")
}
}

这就是我运行项目时的样子。看起来就应该如此

enter image description here

我以这种方式调用 Viewcontroller 内的自定义注释

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation
{
return nil
}
var annotationView = self.mapView.dequeueReusableAnnotationView(withIdentifier: "Pin")
if annotationView == nil{
annotationView = CustomAnnotation(annotation: annotation, reuseIdentifier: "Pin")
annotationView?.canShowCallout = false
} else {
annotationView?.annotation = annotation
}

return annotationView
}

最佳答案

问题是,您正在创建一个 MKPinAnnotation View ,并在其上添加两个额外的 View 。您应该为此使用标注 View 。在您现有的代码中,鼠标单击只会“穿过”引脚,尝试向下移动按钮以覆盖引脚,然后您会看到选择器(需要更新)将被调用。

如果您只想快速显示一个按钮,请考虑将其添加为标注附件 View ,如下所示(在 ViewCtrl 中):

if annotationView == nil{
annotationView = CustomAnnotation(annotation: annotation, reuseIdentifier: "Pin") //Might as well be MKPinAnnotationView
annotationView?.canShowCallout = true

let infoButton = UIButton(type: .detailDisclosure)

infoButton.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
infoButton.backgroundColor = UIColor.red
infoButton.layer.borderColor = UIColor.darkGray.cgColor
infoButton.layer.cornerRadius = 5
infoButton.layer.masksToBounds = true

infoButton.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)

annotationView?.leftCalloutAccessoryView = infoButton

}

然后记住添加操作:

    func buttonClicked() {
print("Button Clicked")
}

结果:

enter image description here

编辑:看一下 Apple 的“MapCallouts”示例代码,它们显示了 iOS 和 macOS 的不同类型的注释。非常有用!

关于ios - 自定义注释内的按钮不会使用react,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42605810/

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