iOS-MKAnnotationView自定义UI按钮问题
将自定义iOS-MKAnnotationView自定义UI按钮问题,ios,uibutton,mkmapview,mkannotationview,Ios,Uibutton,Mkmapview,Mkannotationview,将自定义ui按钮添加到MKAnnotationView后,按钮的UIControlEventTouchUpInside不工作 请查看下面我的代码: - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { MKAnnotationView *annotationView = (MKAnnotationView *) [self.ma
ui按钮添加到MKAnnotationView
后,按钮的UIControlEventTouchUpInside
不工作
请查看下面我的代码:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *annotationView = (MKAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"myIdentifier"];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image = [UIImage imageNamed:@"location-pin"];
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(0, 0, 150, 30);
[rightButton setTitle:annotationText.userName forState:UIControlStateNormal];
[rightButton setBackgroundColor:[UIColor clearColor]];
rightButton.layer.borderColor = [UIColor blackColor].CGColor;
rightButton.showsTouchWhenHighlighted = YES;
[rightButton addTarget:self action:@selector(rightAcccoryViewButtonCLicked:) forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = rightButton;
}
else {
annotationView.annotation = annotation;
}
return annotationView;
}
return nil;
}
在中心点按时,除了添加到右侧调用访问视图
之外,是否无法获取操作
非常感谢您的帮助
谢谢。试试这个
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[self.mapview dequeueReusableAnnotationViewWithIdentifier:@"Pin"];
if(pinView == nil)
{
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"] ;
}
UIButton *detailButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 250, 100)];
[detailButton addTarget:self action:@selector(addToListButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[detailButton setSelected:NO];
pinView.leftCalloutAccessoryView = detailButton;
[pinView setImage:[UIImage imageNamed:@"locationPin.png"]];
[pinView setFrame:CGRectMake(0, 0, 55, 55)];
pinView.animatesDrop = NO;
pinView.canShowCallout = YES;
// [pinView setSelected:YES animated:YES];
return pinView;
}
-(void)addToListButtonTapped:(id)sender
{
int temp =2;
AnnotationViewController *ann1 =[sender superview];
NSString *str = ann1.title;
NSLog(@"button tapped");
NSLog(@"%@",str);
}
-(MKAnnotationView*)地图视图:(MKMapView*)mV视图用于注释:
(id)注释{
MKPinAnnotationView*pinView=(MKPinAnnotationView*)[self.mapview出列可重用AnnotationView,标识符:@“Pin”];
如果(pinView==nil)
{
pinView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:@“Pin”];
}
UIButton*detailButton=[[UIButton alloc]initWithFrame:CGRectMake(0,0,250,100)];
[detailButton addTarget:self action:@selector(addToListButtonTapped:)for ControlEvents:UIControlEventTouchUpInside];
[选择的详细按钮:否];
pinView.leftCalloutAccessoryView=detailButton;
[pinView setImage:[UIImage ImageName:@“locationPin.png”];
[pinView设置帧:CGRectMake(0,0,55,55)];
pinView.animatesDrop=否;
pinView.canShowCallout=是;
//[pinView setSelected:是动画:是];
返回pinView;
}
-(无效)addToListButtonTapped:(id)发件人
{
内部温度=2;
AnnotationViewController*ann1=[sender superview];
NSString*str=ann1.title;
NSLog(“点击按钮”);
NSLog(@“%@”,str);
}
(i)。首先尝试使用圆角矩形按钮,然后使用自定义按钮。
(ii)。然后,检查图像名称是否与资源文件夹中的名称相同但按钮仍使用RightCallout AccessoryView进行初始化。当我点击中心时,什么也没有发生。使用我编辑的答案,并根据需要更改按钮框,效果很好。谢谢!但是我如何从addToListButtonTapped获取注释值呢?谢谢。最后一个问题-我按钮的标题显示了两次。为什么会这样?原来的按钮标题应该是:“你叫什么名字…”,但我得到的是“你叫什么名字..什么”。增加你的按钮宽度或不设置你的按钮标题
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[self.mapview dequeueReusableAnnotationViewWithIdentifier:@"Pin"];
if(pinView == nil)
{
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"] ;
}
UIButton *detailButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 250, 100)];
[detailButton addTarget:self action:@selector(addToListButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[detailButton setSelected:NO];
pinView.leftCalloutAccessoryView = detailButton;
[pinView setImage:[UIImage imageNamed:@"locationPin.png"]];
[pinView setFrame:CGRectMake(0, 0, 55, 55)];
pinView.animatesDrop = NO;
pinView.canShowCallout = YES;
// [pinView setSelected:YES animated:YES];
return pinView;
}
-(void)addToListButtonTapped:(id)sender
{
int temp =2;
AnnotationViewController *ann1 =[sender superview];
NSString *str = ann1.title;
NSLog(@"button tapped");
NSLog(@"%@",str);
}