我正在尝试通过单击地图标注打开新视图。
在 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!) {
}
我环顾四周,但我能找到的唯一答案并不能真正解决我的问题。
感谢您的帮助!