iOS-在 MKAnnotationView 上获取按钮以删除 pin
我创建了一个 MKAnnotationView 并在其上添加了一个 UIButton 。现在我希望该按钮删除它所在的引脚。
这就是我添加按钮的方式:
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinAnnotation = nil;
if(annotation != myMapView.userLocation)
{
NSString *defaultPinID = [[NSString alloc] initWithFormat:@"pin%d",pinCount];
pinAnnotation = (MKPinAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinAnnotation == nil )
pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinAnnotation.canShowCallout = YES;
//// ADDING MY BUTTON
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinAnnotation.rightCalloutAccessoryView = deleteButton;
//// ADDING IT'S ACTION
[deleteButton addTarget:self action:@selector(deletePin) forControlEvents:UIControlEventTouchUpInside];
}
pinCount++;
return pinAnnotation;
}
如您所见,我设置了调用此方法的操作:
-(void)deletePin
{
//What to put here...?
}
如何使该操作删除其按钮的引脚?
我想也许可以通过发送id
之类的东西,但我不确定如何发送。
I have created a MKAnnotationView
and do add a UIButton
on it. Now I would like that button to remove the pin it is in.
This is how I add the button:
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinAnnotation = nil;
if(annotation != myMapView.userLocation)
{
NSString *defaultPinID = [[NSString alloc] initWithFormat:@"pin%d",pinCount];
pinAnnotation = (MKPinAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinAnnotation == nil )
pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinAnnotation.canShowCallout = YES;
//// ADDING MY BUTTON
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinAnnotation.rightCalloutAccessoryView = deleteButton;
//// ADDING IT'S ACTION
[deleteButton addTarget:self action:@selector(deletePin) forControlEvents:UIControlEventTouchUpInside];
}
pinCount++;
return pinAnnotation;
}
As you can see there, I set the action to call this method:
-(void)deletePin
{
//What to put here...?
}
How can I make that action delete it's button's pin?
I figured maybe sending the id
thru or something, but am not sure how.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于注释标注附件视图,请勿使用您自己的目标/操作方法。
相反,使用地图视图的 calloutAccessoryControlTapped 委托方法,这将使这项工作变得更加容易。
删除
addTarget
行和您的自定义方法。然后实现委托方法:另外,不相关,但您设置重用标识符的方式是错误的,并且通过为每个注释分配不同的 id 来破坏可重用性。推荐如下:
With annotation callout accessory views, do not use your own target/action method.
Instead, use the map view's
calloutAccessoryControlTapped
delegate method which will make this job much easier.Remove the
addTarget
line and your custom method. Then implement the delegate method instead:Also, unrelated, but the way you are setting the re-use identifier is wrong and defeats re-usability by assigning a different id to every annotation. The following is recommended:
在回调中获取发送者
然后获取与 MKPinAnnotationView 关联的 MKAnnotation,将其从 MKMapView 的注释中删除并重绘 (
[mapView setNeedsDisplay]
)Get the sender in your callback
Then get the MKAnnotation associated with the MKPinAnnotationView, remove it from the MKMapView's annotations and redraw (
[mapView setNeedsDisplay]
)