我正在尝试通过点击地图标注来打开新视图。
在obj-C中我的代码是这样的:
//Callout
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
if ([view.annotation isKindOfClass:[Annotation class]])
{
Annotation *myAnn = (Annotation *)view.annotation;
id vcToPush = nil;
if ([[myAnn title] isEqualToString:@"View1"]){
vcToPush = [[View1 alloc]init];
}
if ([[myAnn title] isEqualToString:@"View2"]){
vcToPush = [[View2 alloc]init];
}
if ([[myAnn title] isEqualToString:@"View3"]){
vcToPush = [[View3 alloc]init];
}
[self.navigationController pushViewController:vcToPush animated:YES];
}
}
Swift中的等价物是什么?
目前我已经有了这个,但我被困住了:
//Pin type and callout
func mapView(_mapView: MKMapView!,
viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if annotation is MKUserLocation {
//return nil so map view draws "blue dot" for standard user location
return nil
}
let reuseId = "pin"
var pinView = MapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
var rightCalloutAccessoryView: UIView!
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.pinColor = .Red
pinView!.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as UIButton
}
else {
pinView!.annotation = annotation
}
return pinView
}
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
}
我看了一眼,但我能找到的唯一答案并不能解决我的问题 谢谢你的帮助!
答案 0 :(得分:3)
试试这个:
func mapView(MapView: MKMapView!, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if control == annotationView.rightCalloutAccessoryView {
performSegueWithIdentifier("NameOfYourSegue", sender: self)
println("Going to the next VC!")
}
}
答案 1 :(得分:1)
可能是这样的。如果您使用标题作为标识符,则不需要验证它是否是您的自定义类,因为它也有标题。
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView,
calloutAccessoryControlTapped control: UIControl) {
let location = view.annotation as! MKAnnotation
if (location.title == "Name 1") {
performSegueWithIdentifier("Segue1", sender: self)
} else if (location.title == "Name 2") {
performSegueWithIdentifier("Segue2", sender: self)
}
}