【问题标题】:viewForAnnotation method never calledviewForAnnotation 方法从未被调用
【发布时间】:2013-01-27 18:05:22
【问题描述】:

我已经实现了我的MKMapViewDelegate,但由于某种原因,我的viewForAnnotation 方法从未被调用过。

我可以确认 MapView 上显示了注释。

我在我的方法中添加了一个NSLog(),并且从未打印过日志记录语句。

我知道我的类肯定是​​MKMapViewDelegate,因为其他MKMapViewDelegate 方法中的NSLog() 语句被打印出来了。

我也在viewDidLoad中设置self.mapView.delegate = self

@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

【问题讨论】:

  • 你在.h文件中添加函数了吗?
  • 你能用updateMapView方法的实现来修正你的问题吗?您能否确认已将注释正确添加到地图视图中?
  • 我已经添加了 updateMapView 方法。我可以确认注释确实已经出现在屏幕上。

标签: ios mkmapview mkmapviewdelegate


【解决方案1】:

我认为您忘记将代理出口连接到文件所有者。右键单击它并将其委托出口连接到文件所有者。

【讨论】:

    【解决方案2】:

    一个可能的解释是没有注释。

    检查实际将注释添加到mapView 的代码。

    【讨论】:

    • 感谢您的建议。我的 MapView 肯定会呈现注释,因为我可以在地图上看到它们。但是,每当我尝试使用自己的 viewForAnnotation() 实现覆盖注释的默认视图时,它都会被忽略。
    • 尝试在 IB 中分配委托,而不是以编程方式进行
    • 如何在 Interface Builder 中分配委托?我正在使用带有情节提要的 XCode 4.5。
    • Ctrl+单击(或右键单击)地图并将线拖到IB中的File's Owner对象。
    • 我认为这在 XCode 4.5 中有点不同,因为我在 IB 中看不到 File's Owner 对象。
    猜你喜欢
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 2014-03-04
    相关资源
    最近更新 更多