Ios 从未调用viewForAnnotation方法
我已经实现了我的Ios 从未调用viewForAnnotation方法,ios,mkmapview,mkmapviewdelegate,Ios,Mkmapview,Mkmapviewdelegate,我已经实现了我的MKMapViewDelegate,但由于某种原因,从未调用我的viewForAnnotation方法 我可以确认注释是否显示在地图视图上 我在方法中添加了一个NSLog(),日志语句从未打印 我知道我的类肯定是MKMapViewDelegate,因为其他MKMapViewDelegate方法中的NSLog()语句是打印出来的 我还在viewDidLoad @interface MapViewController () <MKMapViewDelegate>
MKMapViewDelegate
,但由于某种原因,从未调用我的viewForAnnotation
方法
我可以确认注释是否显示在地图视图上
我在方法中添加了一个NSLog()
,日志语句从未打印
我知道我的类肯定是MKMapViewDelegate
,因为其他MKMapViewDelegate
方法中的NSLog()
语句是打印出来的
我还在viewDidLoad
@interface MapViewController () <MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end
@implementation MapViewController
@synthesize mapView = _mapView;
@synthesize annotations = _annotations;
-(void)setMapView:(MKMapView *)mapView
{
_mapView = mapView;
[self updateMapView];
}
-(void)setAnnotations:(NSArray *)annotations
{
_annotations = annotations;
[self updateMapView];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id<MKAnnotation>)annotation
{
// For some reason this method is never called!!!!!!
NSLog(@"This logging statement is never printed");
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate = self;
}
- (void)updateMapView
{
if (self.mapView.annotations) [self.mapView removeAnnotations:self.mapView.annotations];
if (self.annotations) [self.mapView addAnnotations:self.annotations];
}
@end
@接口MapViewController()
@属性(弱,非原子)IBMMapView*mapView;
@结束
@MapViewController的实现
@综合地图视图=_地图视图;
@综合注释=_注释;
-(void)setMapView:(MKMapView*)mapView
{
_mapView=mapView;
[self-updateMapView];
}
-(void)setAnnotations:(NSArray*)注释
{
_注释=注释;
[self-updateMapView];
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图
viewForAnnotation:(id)注释
{
//由于某些原因,此方法从未被调用!!!!!!
NSLog(@“此日志记录语句从未打印”);
}
-(无效)viewDidLoad
{
[超级视图下载];
self.mapView.delegate=self;
}
-(无效)updateMapView
{
if(self.mapView.annotations)[self.mapView removeAnnotations:self.mapView.annotations];
if(self.annotations)[self.mapView addAnnotations:self.annotations];
}
@结束
我想您忘了将代理出口连接到文件所有者。右键单击它并将其代理出口连接到文件所有者。可能的解释是没有注释
检查实际将注释添加到
mapView
的代码。是否在.h文件中添加了该函数?是否可以使用updateMapView
方法来修改您的问题?能否确认已将注释正确添加到地图视图中?我已添加updateMapView方法。我可以确认注解已经出现在屏幕上。谢谢你的建议。我的MapView肯定在渲染注释,因为我可以在地图上看到它们。但是,每当我尝试使用自己的viewForAnnotation()实现覆盖注释的默认视图时,它都会被忽略。请尝试在IB中分配委托,而不是以编程方式分配委托如何在Interface Builder中分配委托?我将XCode 4.5与故事板一起使用。在地图上按住Ctrl键并单击(或右键单击),然后将线条拖到IB中的文件的所有者
对象。我认为这在XCode 4.5中的效果有所不同,因为我在IB中看不到文件的所有者对象。