MKMapViewコールアウトアノテーションにアクセサリビューボタンを追加しますか?
-
07-07-2019 - |
質問
最近このウェブサイトそして、コールアウトビューにボタンを(画像から)追加しようとしています。
ウェブサイトの例のコードは正常に機能しますが、同じコードをプロジェクトに追加しようとしても同じ結果が得られません。
見逃したことは明らかにありますが、うまくいかないようです!
注釈を付けるための私のコードは次のとおりです。
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
annView.pinColor = MKPinAnnotationColorGreen;
UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeCustom];
advertButton.frame = CGRectMake(0, 0, 23, 23);
advertButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
advertButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[advertButton setImage:[UIImage imageNamed:@"button_right.png"] forState:UIControlStateNormal];
[advertButton addTarget:self action:@selector(showLinks:) forControlEvents:UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView = advertButton;
annView.animatesDrop=TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
return annView;
}
ご協力いただければ幸いです! :)
解決 3
自分の質問に答えて申し訳ありませんが、MKMapViewのデリゲートをViewControllerに設定することで解決しました。ちょっと、それを理解するのにしばらくかかりました、それはとても簡単です!!
他のヒント
あの男は、カスタム画像を使用して詳細開示を模倣しています。実際に、標準の詳細情報を非常に簡単に入手できます。
[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
ここで行ったようにターゲットを設定する代わりに:
[advertButton addTarget:self action:@selector(showLinks:) forControlEvents:UIControlEventTouchUpInside];
デフォルトのデリゲートを使用できます:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
所属していません StackOverflow