如何在Swift中使Map注释调出打开一个新的视图控制器



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

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

试试这个:

func mapView(MapView: MKMapView!, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    if control == annotationView.rightCalloutAccessoryView {
        performSegueWithIdentifier("NameOfYourSegue", sender: self)
        println("Going to the next VC!")
    }
}

可能是这样的。如果您使用title作为标识符,则不需要验证它是否是您的自定义类,因为它也有title。

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)
        }  
}

相关内容

  • 没有找到相关文章

最新更新