我在UI按钮中继承了注释类,如:
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'没有会员' 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.
}
答案 0 :(得分:0)
如果您使用的是@objc
并且已在班级func TaskDetail(_sender: MapButton)
内宣布,请务必在Swift 4
之前添加MapController
。
例如
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
}
我希望这有帮助!