objective-c - 重置标题/副标题后如何调整标注气泡的大小

标签 objective-c annotations title mkannotationview callouts

我创建了一个 MKAnnotation 名称 PushPin,它有一个标题和副标题。我希望以后能够动态更改标题。我很接近,所以我宁愿不必制作一个全新的 AnnotationView,但如果我必须这样做,我想那也没关系。我的问题是,一旦我更改了标题的文本,窗口就不会调整大小,并且某些文本可能会被截断,具体取决于标题的原始大小。

1) 是否有可以触发的事件来再次调整标注气泡窗口的大小?

2) 另外,在重置标题之前,我先检查以确保注释确实有标题,但是检查后我在转换它时遇到了一些问题,有人可以帮我解决这个问题吗?我对 objective-c 还是个新手,这个问题困扰了我一段时间。


#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?
 }

}

最佳答案

仍有一些问题,但可能会越来越近。我试过了,但还是没有运气。我部分借鉴了 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]];
}

好消息是,我为其他可能好奇的人解决了我的类型转换问题。

((PushPin *)view.annotation).title = @"test";

关于objective-c - 重置标题/副标题后如何调整标注气泡的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3928860/

相关文章:

ios - 未收到 NSNotification

objective-c - UIBezierPath 绘制直线

iphone - 如果没有更好的代码检查方式,我该如何编写呢?

java - 内部源保留注释的表示

python - 抽象类子类的函数注解

javascript - 如何通过函数动态更改文档标题?

objective-c - 如何扩展 NSProgressIndicator 并增加大小

java - JSR-303 - 计算值作为参数?

html - 如何在保持大小和对齐的同时将 h2 标题更改为 <p> 文本?