Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6229421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:33:44+00:00 2026-05-24T09:33:44+00:00

Anyone know if there’s a way to get click event from a button that

  • 0

Anyone know if there’s a way to get click event from a button that is added to MKAnnotationView, this button is used as label just to display the name of each pin on the map , now I successd to show a custom view (which contains image, text ….) when the pin is clicked so i need to do the same thing when the button (label) is clicked.

Thanks for any advice you can provide.

code for button in MKAnnotationView:

UIButton * pinButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 140, 28)];
[pinButton.titleLabel setTextColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1]]; 
[pinButton setCenter:CGPointMake(pinAnnotationView.center.x + 70, pinAnnotationView.center.y + 10)]; 
[pinButton addTarget:self action:@selector(pinLabelClicked) forControlEvents:UIControlEventTouchUpInside]; 
[pinAnnotationView addSubView:pinButton]; 
[pinButton setUserInteractionEnabled:YES];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:33:45+00:00Added an answer on May 24, 2026 at 9:33 am

    The standard UI approach is to use the callout view and add an accessory button as progrmr shows.

    However, if you must add a button directly to the MKAnnotationView, the problems with your approach are that the MKPinAnnotationView‘s default frame (which can’t easily be changed) is smaller than the button you’re adding so most of the button will not respond to touches and even if you switch to using an MKAnnotationView and increase the frame size, the MKMapView will prevent the button from getting any touches.

    What you’ll need to do is add a UITapGestureRecognizer to the button (use the gesture handler’s action method instead of an addTarget on the button) and add the button to a plain MKAnnotationView with an appropriate frame size instead of an MKPinAnnotationView.

    Example:

    - (MKAnnotationView *)mapView:(MKMapView *)mapView 
            viewForAnnotation:(id<MKAnnotation>)annotation
    {
        MKAnnotationView *annView = (MKAnnotationView *)[mapView 
                dequeueReusableAnnotationViewWithIdentifier: @"pin"];
        if (annView == nil)
        {
            annView = [[[MKAnnotationView alloc] initWithAnnotation:annotation 
                          reuseIdentifier:@"pin"] autorelease];
    
            annView.frame = CGRectMake(0, 0, 200, 50);
    
            UIButton *pinButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            pinButton.frame = CGRectMake(0, 0, 140, 28);
            pinButton.tag = 10;
    
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] 
                initWithTarget:self action:@selector(handlePinButtonTap:)];
            tap.numberOfTapsRequired = 1;
            [pinButton addGestureRecognizer:tap];
            [tap release];
    
            [annView addSubview:pinButton]; 
        }
    
        annView.annotation = annotation;
    
        UIButton *pb = (UIButton *)[annView viewWithTag:10];
        [pb setTitle:annotation.title forState:UIControlStateNormal];
    
        return annView;
    }
    
    - (void) handlePinButtonTap:(UITapGestureRecognizer *)gestureRecognizer 
    {
        UIButton *btn = (UIButton *) gestureRecognizer.view;
        MKAnnotationView *av = (MKAnnotationView *)[btn superview];
        id<MKAnnotation> ann = av.annotation;
        NSLog(@"handlePinButtonTap: ann.title=%@", ann.title);
    }
    

    Note that this will prevent the map view’s didSelectAnnotationView delegate method from firing. If you need that method to fire (in addition to the button’s gesture handler method), then add the following:

    //in the view controller's interface:
    @interface YourVC : UIViewController <UIGestureRecognizerDelegate>
    
    //where the UITapGestureRecognizer is created:
    tap.delegate = self;
    
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
            shouldRecognizeSimultaneouslyWithGestureRecognizer
                :(UIGestureRecognizer *)otherGestureRecognizer
    {
        return YES;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know there have any other way that (by not using json_encode and
Does anyone know whether there's a way to mock Entity Data Provider so Unit
Does anyone know if there's an add-in that does autocomplete for queries on SQL
Does anyone know if there is an API to get the current monitor state
Does anyone know if there's a way to automatically expand a list in Python,
Does anyone know if there is a way (or tool) we can use to
I am trying to catch the back button event for Android. I know there
Does anyone know if you can access the databound event from a databound, say,
Anyone know if there is an equivalent method in jquery or javascript that matches
Does anyone know how to read a x.properties file in Maven. I know there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.