我需要从 MKAnnotationView 派生一个新类,就像在 WeatherMap 中一样来自 Apple 的项目,但图形部分通过 NIB 文件定义:有没有办法做到这一点?我必须创建一个新的 Controller 吗?
最佳答案
一种方法是从 nib 加载 View ,然后将 View 作为 subview 添加到 MKAnnotationView。您不需要创建任何新的 Controller 。
首先,创建您的 MKAnnotationView 子类,其中包含您将加载 NIB 的自定义 View 的属性:
@interface MapAnnotationView : MKAnnotationView
@property (nonatomic, retain) IBOutlet UIView* loadedView;
然后,在此类的 init 方法中,加载 nib 并添加为 subview :
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString*)reuseIdentifier
{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self != nil)
{
[[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil];
if (loadedView){
[self addSubview:loadedView];
}
}
return self;
}
最后,创建您的 MyCustomView NIB(作为 UIView)。将文件的所有者设为上述 MapAnnotationView 类,并将 View 附加到上面定义的 IBOutlet。
就是这样。
关于ios - 派生 MKAnnotationView 并将其链接到 NIB 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6984638/