1

我正在尝试通过单击地图标注打开新视图。
在 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!) {

}

我环顾四周,但我能找到的唯一答案并不能真正解决我的问题。
感谢您的帮助!

4

2 回答 2

3

尝试这个:

func mapView(MapView: MKMapView!, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

    if control == annotationView.rightCalloutAccessoryView {
        performSegueWithIdentifier("NameOfYourSegue", sender: self)
        println("Going to the next VC!")
    }
}
于 2015-02-25T10:57:44.787 回答
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)
        }  
}
于 2015-10-28T20:59:57.263 回答