简体   繁体   中英

Button inside custom Callout View not responding (Swift)

I have a custom MKPinAnnotationView created as so:

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

    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView!.canShowCallout = false
        pinView!.animatesDrop = true
        pinView!.pinColor = .Red
        pinView!.draggable = true;
    } else {
        pinView!.annotation = annotation
    }

    return pinView
}

Then I add a subview to it later with a button like this:

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
    view.addSubview(confirm)
}

The problem is that the button inside the subview is not responsive at all. What could be causing that? I already tried playing around with userInteraction enabled. Thanks

Not sure if this fits or helps but in my case I where I have similar code I set

pinView!.canShowCallout = true

and then if you want a simple way to catch the button clicks I put a func inside of my ViewContriller:

func buttonClicked (sender : UIButton!) {
    println("Button Clicked")
}

It sounds like you want to do something a bit different though.

Opps.. in previous answer I forgot that I added the button just above the

return pinView

....

    let button : UIButton = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as UIButton
    button.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
    pinView!.rightCalloutAccessoryView = button
    return pinView

Now the buttonClicked func can catch the callout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM