1

I'm trying to call the mapView(mapView: MKMapView, didDeselectAnnotationView view: MKAnnotationView) function programmatically using this code:

dispatch_async(dispatch_get_main_queue()) {
     for item in self.largeMapView.selectedAnnotations {
           self.largeMapView.deselectAnnotation(item, animated: false)
 }
}

but this doesn't do anything. If I press on the map, it is called, but I want it to be called without the need to press.

Alk
  • 5,215
  • 8
  • 47
  • 116
  • i dont understand what you want, use English please – Mazel Tov Aug 16 '16 at 19:21
  • I want to dismiss the currently selected annotation on my `MKMapView`. Normally this happens when the user presses outside of the selected pin, anywhere else on the mapView. However, I want to trigger this in code, so that the user doesn't need to press anywhere. – Alk Aug 16 '16 at 19:22
  • I want to dismiss the callout, and 'deselect' the pin, so that the next time I press on the pin, it becomes selected again. I don't want to remove the pin from the map or anything like that. – Alk Aug 16 '16 at 19:37
  • The code in your question works for me. I assume you're doing the standard callout? I might add a breakpoint or print statement inside that loop, and make sure it's (a) getting to this code; and (b) finding the selected annotation on `largeMapView`. The only thing that I can imagine is if the `largeMapView` reference didn't match the map in the view hierarchy or if you did some fairly unusual in generating callouts (e.g. disabling the standard callout and writing your own code to create a custom callout). But I cannot reproduce your problem on the basis of what's been provided thus far. – Rob Aug 16 '16 at 20:30
  • Hi, yes I use a custom callout. I can provide the code for that if it's necessary, although the custom callout gets dismissed normally if I click anywhere on the map so I didn't think that's relevant to the problem. – Alk Aug 17 '16 at 09:42
  • The problem doesn't rest in the above code, so it's got to be something else, and custom callouts is one possible (albeit unlikely) source of the problem. For example, in Apple's documentation on a completely custom callout (i.e. not just a system callout with customization of the various accessory views, but a completely replacement of the system callout), they suggest dismissing the callout by doing a hit test, and if you did that, that would result in the behavior you describe. (Personally, I remove custom callouts in `didDeselect...`, to avoid precisely this sort of problem.) – Rob Aug 17 '16 at 16:32
  • But, bottom line, I'm just trying to reverse engineer what would result in the behavior you describe, because the above code, alone, wouldn't. There's got to be something else going on and custom callouts is merely one candidate problem. But, really, you should create a [MCVE](http://stackoverflow.com/help/mcve) from scratch and post a simple example that reproduces the problem you describe. Until we can reproduce your problem, it's hard to help you solve it. (And frankly, in the process of creating a MCVE from scratch, you are likely to figure out what the source of the problem is yourself.) – Rob Aug 17 '16 at 16:34

1 Answers1

1

Not exactly sure why, but this code in full seems to solve the problem :

dispatch_async(dispatch_get_main_queue()) {
    for item in self.largeMapView.selectedAnnotations {
    self.largeMapView.deselectAnnotation(item, animated: false)
 }
   myView.pinTintColor = UIColor.greenColor()
   calloutView.hidden = true
}

Manually changing the pinTintColor to the "deselected" one, and manually hiding the calloutView solved the problem.

Alk
  • 5,215
  • 8
  • 47
  • 116