I created an MKAnnotation name PushPin which has a title and subtitle. I want to be able to dynamically change the title at a later time. I'm close so I'd rather not have to make a whole new AnnotationView, but if I have to I guess that's ok too. My problem is that, once I change the text for the title, the window does not resize and some text might get cut off depending on how big the title originally was.
1) Is there an event I can trigger to resize the callout bubble window again?
2) Also, I check to make sure that the annotation actually has a title first before I go resetting the title, but I had some trouble casting it after I checked, can someone help me out with this? I'm still new to objective-c and this one messed with me for a while.
#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
@interface PushPin : NSObject <MKAnnotation> {
CLLocationCoordinate2D _coordinate;
NSString *_title;
NSString *_subtitle;
NSString *_ID;
}
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *subtitle;
@property (nonatomic, retain) NSString *ID;
- (id) initWithCoordinateAndInformation:(CLLocationCoordinate2D)coordinate title:(NSString *)title subtitle:(NSString *)subtitle;
@end
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
NSLog(@"Annotation was TAPPED!");
if ([view.annotation isKindOfClass:[PushPin class]]) {
view.annotation.title = @"test";
// warning here, that this might not be implemented...
// but it is for this class type, how do I cast it to the correct type?
}
}
Still having some problems, but getting closer maybe. I tried this, but still no luck. I am partially drawing on the code from http://digdog.tumblr.com/post/252784277/mapkit-annotation-drag-and-drop-with-callout-info
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
NSLog(@"Annotation was TAPPED!");
if ([view.annotation isKindOfClass:[PushPin class]]) {
((PushPin *)view.annotation).title = @"test";
}
[self willChangeValueForKey:@"subtitle"]; // Workaround for SDK 3.0, otherwise callout info won't update.
[self didChangeValueForKey:@"subtitle"]; // Workaround for SDK 3.0, otherwise callout info won't update.
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"MKAnnotationCalloutInfoDidChangeNotification" object:self]];
}
Good news is that, I figured out my casting problem, for others who may be curious.
((PushPin *)view.annotation).title = @"test";
来源:https://stackoverflow.com/questions/3928860/how-to-resize-callout-bubble-after-resetting-title-subtitle