Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
568 views
in Technique[技术] by (71.8m points)

ios - Button action in mkannotation view not working?

Hello i have stuck in one situation on map where i have managed to show custom annotation. When the annotation is tapped i have to show a view with some information and one button. when user taps on the button then one alert should display.

code is as follows

 - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
       if (![view.annotation isKindOfClass:[MKUserLocation class]]) {

          POCustomAnnotation *annotation = (POCustomAnnotation *)[view annotation];

          UIView *bgview=[[UIView alloc]initWithFrame:CGRectMake(-30, -150,130, 150)];
          bgview.backgroundColor=[UIColor grayColor];
          UILabel *templbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 130, 120)];

          templbl.text=annotation.displayText;
          templbl.textAlignment=NSTextAlignmentRight;
          templbl.numberOfLines=10;

          [bgview addSubview:templbl];

          UIButton *tembtn=[UIButton buttonWithType:UIButtonTypeCustom];        
          [tembtn setTitle:@"More info" forState:UIControlStateNormal];
          [tembtn addTarget:self action:@selector(annotationBtnAction:)     
           forControlEvents:UIControlEventTouchUpInside];         
          tembtn.backgroundColor=[UIColor grayColor];   
           tembtn.frame=CGRectMake(0, 121, 130, 30);       
          [bgview addSubview:tembtn];


          [view addSubview:bgview];
    } else {

          POMapAnnotationView *callout = (POMapAnnotationView *)[[[NSBundle mainBundle]loadNibNamed:@"POMapAnnotationView" owner:self options:nil] objectAtIndex:0];
          view.canShowCallout = NO;
          CGRect calloutViewFrame = callout.frame;
          calloutViewFrame.origin = CGPointMake(-calloutViewFrame.size.width / 2 + 15, - calloutViewFrame.size.height);
          callout.frame = calloutViewFrame;
          [callout setLabelText:[self distanceText]];
          [view addSubview:callout];
     }
 }

I am trying to handle this

 [tembtn addTarget:self action:@selector(annotationBtnAction:)     
              forControlEvents:UIControlEventTouchUpInside]; 

which is not getting called. and my screen looks like this

enter image description here

Thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to set frame for this button. which is not really seen in the code. Adjust frame as you want by x position and y position.

UIButton *tembtn=[UIButton buttonWithType:UIButtonTypeCustom];

// Frame required to render on position and size. Add below line 
[tembtn setFrame:CGRectMake(0, 0, 100, 50)];

[tembtn setTitle:@"More info" forState:UIControlStateNormal];
[tembtn addTarget:self action:@selector(annotationBtnAction:)
 forControlEvents:UIControlEventTouchUpInside];
tembtn.backgroundColor=[UIColor grayColor];
[view addSubview:tembtn];

Edit:

Found the reason you can only tap on area, in which annotation is rendered.

Here is simple demo for custom call out:

Custom Callout Demo

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...