簡體   English   中英

從未調用過MKAnnotationView viewForAnnotation

[英]MKAnnotationView viewForAnnotation never called

在我花了2天的時間搜索這個bug后,我必須在這里尋求幫助。 我有MapViewController並在地圖上放置一些引腳。 我已經從AppleCallouts和WeatherMap復制了蘋果代碼示例中的大部分代碼。

無論如何,我似乎刪除或遺漏了必要的部分。 似乎MapViewController和以下代碼之間沒有任何關聯

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    NSLog(@"MKAnnotationView");
    return nil;
}

設置注釋看起來像這樣,它運作良好:

- (void)createPoi:(CLLocationCoordinate2D)theCoordinate
{
    NSLog(@"createPoi");

    RandomAnnotation *randomAnnotation = [[RandomAnnotation alloc] init];
    [randomAnnotation setTheCoordinate:theCoordinate];
    [randomAnnotation setTheTitle:@"bla"];
    [randomAnnotation setTheSubTitle:@"bla"];
    [self.mapAnnotations insertObject:randomAnnotation atIndex:kRandomAnnotationIndex];
    [randomAnnotation release];
    [self.mapView addAnnotation:[self.mapAnnotations objectAtIndex:kRandomAnnotationIndex]];
}

我不知道出了什么問題。 任何人都可以給我一些提示嗎? 我不得不承認我對委托模式沒有任何經驗。

確保已設置地圖視圖的delegate屬性。

如果在IB中創建了地圖,請右鍵單擊它並將其委托出口連接到文件所有者。

如果地圖是在代碼中創建的,請在創建地圖視圖后設置委托:

mapView.delegate = self;

在聲明MKMapView的.h中以及聲明viewForAnnotation方法的位置,請確保在您的類應包含的協議列表中添加MKMapViewDelegate:

@interface myViewController : UIViewController <MKMapViewDelegate> {
    MKMapView *_mapView;
}

然后在viewDidLoad方法中,一定要添加

_mapView.delegate = self;

如果你已經使用它,你也可以在界面構建器中分配mapView的委托!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM