我想在此选定的注释视图上添加单击侦听器,以打开带有注释详细信息的另一个视图控制器。我该怎么办?
@model WebApplication2.Controllers.AppointmentViewModel
@{
ViewBag.Title = "Student";
}
<h2>Student</h2>
<script src="~/Scripts/jquery-1.10.2.js"></script>
@Html.DropDownList("StudentNo", ViewData["studentList"] as List<SelectListItem>, "---Please Select---", new { @class = "form-control selectpicker", id = "studentIdDrp" })
<script>
$(document).ready(function () {
$("#studentIdDrp").val('@Model.FK_StudentId');
});
</script>
已编辑
答案 0 :(得分:1)
通常,您需要添加一个rightCalloutAccessoryView
,然后实现calloutAccessoryControlTapped
,如how do I make a pin annotation callout?所示
但是你说:
我需要使整个标注可点击
MapKit没有委托方法来仅在附件视图上捕获标注本身的拍子。但是您可以添加自己的代表来为您执行此操作。
protocol CustomAnnotationViewDelegate: class {
func didTapCallout(for annotation: MKAnnotation)
}
class CustomAnnotationView: MKPinAnnotationView {
static let preferredReuseIdentifier = Bundle.main.bundleIdentifier! + ".customAnnotationView"
weak var delegate: CustomAnnotationViewDelegate?
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
canShowCallout = true
let tap = UITapGestureRecognizer(target: self, action: #selector(didTapAnnotationView(_:)))
self.addGestureRecognizer(tap)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc func didTapAnnotationView(_ gesture: UITapGestureRecognizer) {
let location = gesture.location(in: self)
// ignore taps on the annotation view, itself
if bounds.contains(location) { return }
// if we got here, we must have tapped on the callout
delegate?.didTapCallout(for: annotation!)
}
}
然后在iOS 11及更高版本中,您可以注册此复用标识符:
override func viewDidLoad() {
super.viewDidLoad()
mapView.register(CustomAnnotationView.self,
forAnnotationViewWithReuseIdentifier: CustomAnnotationView.preferredReuseIdentifier)
}
您的viewFor
可以指定委托:
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation { return nil }
let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier, for: annotation) as! CustomAnnotationView
annotationView.delegate = self
return annotationView
}
}
或者,如果您需要支持11之前的iOS版本,则无需注册重用标识符,但是如果CustomAnnotationView
未能成功出队,则必须自己手动实例化:
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation { return nil }
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: CustomAnnotationView.preferredReuseIdentifier) as? CustomAnnotationView
if annotationView == nil {
annotationView = CustomAnnotationView(annotation: annotation, reuseIdentifier: CustomAnnotationView.preferredReuseIdentifier)
annotationView?.delegate = self
} else {
annotationView?.annotation = annotation
}
return annotationView
}
}
无论哪种方式,您现在都可以使视图控制器符合新的CustomAnnotationViewDelegate
:
extension ViewController: CustomAnnotationViewDelegate {
func didTapCallout(for annotation: MKAnnotation) {
print("tapped callout for \(annotation)")
}
}
但是请注意,在上文中,我在CustomAnnotationView
init
方法中添加了轻击手势识别器,以确保在注释视图为首先创建。