iOS-在 MKAnnotationView 上获取按钮以删除 pin

发布于 2024-12-12 05:29:02 字数 1293 浏览 0 评论 0原文

我创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长途伴 2024-12-19 05:29:02

对于注释标注附件视图,请勿使用您自己的目标/操作方法。

相反,使用地图视图的 calloutAccessoryControlTapped 委托方法,这将使这项工作变得更加容易。

删除 addTarget 行和您的自定义方法。然后实现委托方法:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
            calloutAccessoryControlTapped:(UIControl *)control
{
    [mapView removeAnnotation:view.annotation];
}

另外,不相关,但您设置重用标识符的方式是错误的,并且通过为每个注释分配不同的 id 来破坏可重用性。推荐如下:

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *pinAnnotationView = nil;
    if (annotation != myMapView.userLocation) 
    {
        NSString *reuseId = @"StandardPin";
        pinAnnotationView = (MKPinAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
        if ( pinAnnotationView == nil )
        {
            pinAnnotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId] autorelease];
            pinAnnotationView.canShowCallout = YES;

            UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            pinAnnotationView.rightCalloutAccessoryView = deleteButton;
        }
        else
            //update annotation property if view is being re-used...
            pinAnnotationView.annotation = annotation;
    }

    return pinAnnotationView;
}

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:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
            calloutAccessoryControlTapped:(UIControl *)control
{
    [mapView removeAnnotation:view.annotation];
}

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:

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *pinAnnotationView = nil;
    if (annotation != myMapView.userLocation) 
    {
        NSString *reuseId = @"StandardPin";
        pinAnnotationView = (MKPinAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
        if ( pinAnnotationView == nil )
        {
            pinAnnotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId] autorelease];
            pinAnnotationView.canShowCallout = YES;

            UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            pinAnnotationView.rightCalloutAccessoryView = deleteButton;
        }
        else
            //update annotation property if view is being re-used...
            pinAnnotationView.annotation = annotation;
    }

    return pinAnnotationView;
}
动次打次papapa 2024-12-19 05:29:02

在回调中获取发送者

[deleteButton addTarget:self action:@selector(deletePin:) forControlEvents:UIControlEventTouchUpInside];

-(void)deletePin:(id)sender
{
    //What to put here...?
}

然后获取与 MKPinAnnotationView 关联的 MKAnnotation,将其从 MKMapView 的注释中删除并重绘 ([mapView setNeedsDisplay])

Get the sender in your callback

[deleteButton addTarget:self action:@selector(deletePin:) forControlEvents:UIControlEventTouchUpInside];

-(void)deletePin:(id)sender
{
    //What to put here...?
}

Then get the MKAnnotation associated with the MKPinAnnotationView, remove it from the MKMapView's annotations and redraw ([mapView setNeedsDisplay])

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文