簡體   English   中英

如何通過發件人獲取注釋參考?

[英]How to get reference on annotation via sender?

我將注釋與UITapGestureRecognizer連接。 我想檢測觸摸。

    if ([annotation isKindOfClass:[Annotation class]])
 {
     NSString * annotationIdentifier = @"UserAnnotationIdentifier";
     CustomAnnotationView * customAnnotationView = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
     if (!customAnnotationView)
     {
         customAnnotationView = [[CustomAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
         UITapGestureRecognizer *tapGesture =
         [[UITapGestureRecognizer alloc] initWithTarget:self
                                                 action:@selector(calloutTapped:)];
         [customAnnotationView addGestureRecognizer:tapGesture];

我使用代碼底部,但在編譯時導致錯誤

  -(void) calloutTapped:(id) sender {
       id<MKAnnotation> annotation = ((MKAnnotationView*)sender.view).annotation;

錯誤:在類型為__strong id的對象上找不到屬性視圖

更改:

-(void) calloutTapped:(id) sender

至:

-(void) calloutTapped:(UITapGestureRecognizer *) sender

那么sender將具有view屬性, 或者首先將senderUITapGestureRecognizer
正確投放:

 -(void) calloutTapped:(id) sender {
    UITapGestureRecognizer *tapGesture = (UITapGestureRecognizer *)sender;
    MKAnnotationView *sendersView = (MKAnnotationView *)tapGesture.view;
    id<MKAnnotation> annotation = sendersView.annotation;
}

暫無
暫無

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

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