[英]How to get sender from the method showViewController:sender:
[英]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
屬性, 或者首先將sender
為UITapGestureRecognizer
。
正確投放:
-(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.