Max1980
Max1980

Reputation: 209

How can I make a Map annotation callout open a new viewcontroller in Swift?

I'm trying to open a new view by clicking on a map callout.
In obj-C my code was this:

//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];
 }
}

What's the equivalent in Swift?
At the moment I've got this, but I'm stuck:

//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!) {

}

I've had a look around but the only answers I can find are not really solving my problem.
Thanx for your help!

Upvotes: 1

Views: 3124

Answers (2)

iyepes
iyepes

Reputation: 145

It could be something like this. If you are using the title as identifier you don't need to verify if it's your custom class, since it also has 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)
        }  
}

Upvotes: 1

Apprentice
Apprentice

Reputation: 39

Try this:

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

    if control == annotationView.rightCalloutAccessoryView {
        performSegueWithIdentifier("NameOfYourSegue", sender: self)
        println("Going to the next VC!")
    }
}

Upvotes: 3

Related Questions