1)我有ViewController和MapKit
1.1)我在Map
中添加了一些引脚class ViewController: UIViewController, MKMapViewDelegate
2)我为自定义引脚标注和注释
编写新类class CustomPointAnnotation: MKPointAnnotation {
class CustomCalloutView: UIView {
4)我在.xib中创建了按钮,此按钮必须执行某些操作,例如
@IBAction func clickTest(sender: AnyObject) {
print("aaaaa")
}
5)此按钮无法正常工作
您可以在Github上看到的所有项目:https://github.com/genFollowMe1/forStack 谢谢,抱歉英语)
答案 0 :(得分:3)
: https://github.com/nfarina/calloutview
<强>更新强>
for swift
将MKAnnotationView子类化为自定义调用并覆盖hitTest:和pointInside:方法。
import UIKit
import MapKit
class CustomCalloutView: MKAnnotationView {
@IBOutlet weak var name: UILabel!
@IBAction func goButton(sender: AnyObject) {
print("button clicked sucessfully")
}
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
let hitView = super.hitTest(point, withEvent: event)
if hitView != nil {
superview?.bringSubviewToFront(self)
}
return hitView
}
override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
let rect = self.bounds
var isInside = CGRectContainsPoint(rect, point)
if !isInside {
for view in subviews {
isInside = CGRectContainsPoint(view.frame, point)
if isInside {
break
}
}
}
return isInside
}
}
答案 1 :(得分:0)
您必须这样做才能将.xib中的按钮引用到相关的.swift文件中。
在ViewController中,你可以这样做:
let button:UIButton!
[...]
button.targetForAction("tappedButton:", withSender: self)
func tappedButton() {
print("Taped !")
}