MKAnnotation not getting selected in iOS5
Not sure why it would work before but the animation probably requires the annotation view to be created and ready which is unlikely immediately after adding the annotation.
What you can do is move the selection to the didAddAnnotationViews
delegate method which should work on all iOS versions:
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
for (MKAnnotationView *av in views) {
if ([av.annotation isKindOfClass:[MapAnnotations class]]) {
MapAnnotations *pushpin = (MapAnnotations *)av.annotation;
if (_this_pushpin_is_the_one_to_select) {
[mapView selectAnnotation:av.annotation animated:YES];
break; //or return;
}
}
}
}
MKPointAnnotation - Default show title and animated?
You named your annotation point
not annotation
:
[mapView selectAnnotation:point animated:YES];
It happens to the best of us.
Wanted: How to reliably, consistently select an MKMapView annotation
I ran into the same problem, but found what seems like a reliable and reasonable solution:
Implement the delegate method mapView:didAddAnnotationViews:. When I tried selecting the annotation directly within the delegate method, the callout dropped with the pin! That looked odd, so I add a slight delay of a half-second.
-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
[self performSelector:@selector(selectInitialAnnotation)
withObject:nil afterDelay:0.5];
}Select the initial annotation as you'd expect, but calling selectAnnotation:animated;
-(void)selectInitialAnnotation {
[self.mapView selectAnnotation:self.initialAnnotation animated:YES];
}
It seems that selectAnnotation:animated:
is not called under some conditions. Compare with MKMapView docs:
If the specified annotation is not onscreen, and therefore does not
have an associated annotation view, this method has no effect.
Reliably selecting an annotion in MKMapView on start (in both ios 6 and ios 5.1)?
I ended up using perform selector with delay on the actual [map selectAnnotation:annotation animated:YES];
It's a work-around but seems to work nicely.
The rightCalloutAccessory button is not shown
- Are you actually going into the condition? Set a breakpoint to check.
- Why create a MKPinAnnotationView at the beginning, before you know the type of annotation?
- You should dequeue your annotationView instead of alloc/initWithAnnotation:reuseIdentifier
- When you reuse your annotations, you should put everything that doesn't change (color, animation, etc) after the alloc init, and not reset them all the time. Otherwise you lose the interest of reusing.
Other than that your code seems fine, and comparing it to mine, I don't see anything obvious. Remark 1 is the most probable. I would set a breakpoint to see if I really go there, see if I can show a leftCalloutAccessoryView instead, use a different pinColor.
EXC_BAD_ACCESS in viewForAnnotation
This line is causing the EXC_BAD_ACCESS:
[mapView selectAnnotation:annotation animated:YES];
You should not try to select an annotation from the viewForAnnotation
delegate method itself.
Remove the call to selectAnnotation:animated:
from the viewForAnnotation
method.
When selectAnnotation:animated:
is called, the map view needs to redraw the annotation view and show its callout. Redrawing the view causes viewForAnnotation
to be called. Since in that method, the code is calling selectAnnotation:animated:
it causes viewForAnnotation
to get called again, and so on resulting in the crash after it runs out of memory for the recursion.
If you want to have an annotation's callout show as soon as it's added to the map, do it in the didAddAnnotationViews
delegate method instead. See MKAnnotation not getting selected in iOS5 for an example of this. Also note that only one annotation can be selected (show its callout) at a time.
Unrelated to the issue but instead of setting a tag on the accessory views like this:
[[pinView rightCalloutAccessoryView] setTag:1]; // Ver Info
[[pinView leftCalloutAccessoryView] setTag:2]; // Hacer Zoom
in calloutAccessoryControlTapped
, you can just directly check if control
itself equals the right or left accessory view. See How to connect map annotation view buttons with database to go to another view? for an example.
MKPointAnnotation: title always visible. Is it possible?
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = coord;
annotationPoint.title = currentTitle;
[mapView addAnnotation:annotationPoint];
[mapView selectAnnotation:annotationPoint animated:NO];
OR
- (void)mapView:(MKMapView *)map didAddAnnotationViews:(NSArray *)views{
for (MKAnnotationView *av in views){
if ([av.annotation isKindOfClass:[MKPointAnnotation class]]){
[mapView selectAnnotation:av.annotation animated:NO];
break;
}
}
}
MKAnnotation Custom Callout button general action -- how to know which Annotation it's coming from? (iPhone)
Instead of adding your own handler method using addTarget
and then using the selectedAnnotations
property to figure out which annotation was tapped, I prefer using the calloutAccessoryControlTapped
delegate method.
In that delegate method, the annotation tapped is accessed using view.annotation
and if you've defined a custom annotation class with additional properties, you can do a cast to access them using (MyAnnotationClass *)view.annotation
.
Related Topics
How to Dismiss a Modal Segue Then Perform Push Segue to New View Controller
Xcode 13 - "Button" Title Not Disappearing
Uitabbar Change Background Color of One UItabbaritem on iOS7
Nstextattachment Image Not Shown in Nstextview (But in Uitextview)
Hls Video Streaming on iOS/Safari
How to Handle a File Sent with 'Open In...' from Another App to My Own iOS App
Xcode Debug View Hierarchy: Unable to Capture View Hierarchy
iOS Framework Does Not Work on Simulator
Swiftui: Navigate to Home Screen After Login Completed. Navigating Views by Button Click
Passing Arguments to @Selector Method
Runtime Error When Using Corefoundation Objects in a Swift Nsobject Subclass
I Want to Draw Custom Shape Like Apple Shape on UIview
Swiftui - How to Blur the Default Background Color of a View
Applying Different Attributes for Different Portions of an Nsattributedstring
Ld: Library Not Found for -Ldoubleconversion React Native 0.59
iOS: Is Possible to Rounder Radius with Different Value in Each Corner