UIButton没有在didSelectAnnotationView上工作

时间:2015-04-01 06:20:12

标签: ios uibutton mkannotationview

您好我在didSelectAnnotationView中有自定义视图,当用户点击pin制作者我正在使用UILabel和UIButton显示自定义视图时一切正常但但UIButton无法点击请告诉我们如何解决此问题。

我的代码。

     -(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view1
     {
            NSString *bult;

       if ([view1.annotation isKindOfClass:[MapPin class]]) {
            MapPin *annot = view1.annotation;
            NSLog(@"tit%@",annot.nsname);
            bult=annot.nsname;

         }


     myview = [[UIView alloc]initWithFrame:CGRectMake(-60, -110, 200, 100)];
     myview.layer.cornerRadius = 10;
     myview.backgroundColor = [UIColor yellowColor];
     myview.userInteractionEnabled = YES;
     [view1 addSubview:test];

     buldingname = [[UILabel alloc] initWithFrame:CGRectMake(5, -15, 150, 80)];
     buldingname.text=bult;
     [buldingname setTextColor:[UIColor blackColor]];
     [buldingname setBackgroundColor:[UIColor clearColor]];
     [buldingname setFont:[UIFont fontWithName: @"Trebuchet MS" size: 14.0f]];
     buldingname.lineBreakMode = NSLineBreakByWordWrapping;
     buldingname.numberOfLines = 3;

     [myview addSubview:buldingname];


    moredetail=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    moredetail.frame= CGRectMake(0, 55, 200, 50);
   [moredetail setTitle:@"Click here for more details" forState:UIControlStateNormal];
   [moredetail addTarget:self action:@selector(nextbtn:) forControlEvents:UIControlEventTouchUpInside];

   [moredetail setExclusiveTouch:YES];
   [myview addSubview:moredetail];


 }

我的呼叫方法。

    - (IBAction)nextbtn:(id)sender
   {
        NSLog(@"click");
   }

我使用上面的代码不能正常工作请告诉我我在哪里做错了如何解决这个问题。

先谢谢。

2 个答案:

答案 0 :(得分:1)

您需要将子视图添加到自定义视图然后再将其工作。

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view1
     {
            NSString *bult;

       if ([view1.annotation isKindOfClass:[MapPin class]]) {
            MapPin *annot = view1.annotation;
            NSLog(@"tit%@",annot.nsname);
            bult=annot.nsname;

         }


     myview = [[UIView alloc]initWithFrame:CGRectMake(-60, -110, 200, 100)];
     myview.layer.cornerRadius = 10;
     myview.backgroundColor = [UIColor yellowColor];
     myview.userInteractionEnabled = YES;
     [view1 addSubview:myview];

     buldingname = [[UILabel alloc] initWithFrame:CGRectMake(5, -15, 150, 80)];
     buldingname.text=bult;
     [buldingname setTextColor:[UIColor blackColor]];
     [buldingname setBackgroundColor:[UIColor clearColor]];
     [buldingname setFont:[UIFont fontWithName: @"Trebuchet MS" size: 14.0f]];
     buldingname.lineBreakMode = NSLineBreakByWordWrapping;
     buldingname.numberOfLines = 3;

     [myview addSubview:buldingname];


    moredetail=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    moredetail.frame= CGRectMake(0, 55, 200, 50);
   [moredetail setTitle:@"Click here for more details" forState:UIControlStateNormal];
   [moredetail addTarget:self action:@selector(nextbtn:) forControlEvents:UIControlEventTouchUpInside];

   [moredetail setExclusiveTouch:YES];
   [myview addSubview:moredetail];


 }

检查此代码

答案 1 :(得分:1)

它无法正常工作,因为mapView仍会处理触摸事件。 只需在:

中添加DetailView
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{

    if ([annotation isKindOfClass:[MKUserLocation class]])
        {
            return nil;
        }

    static NSString *reuseIdentifier = @"Your_identifier";

    MKAnnotationView *view = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseIdentifier];
    if (view == nil)
    {
        view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
        view.image = [UIImage imageNamed:@"your_annotaion_image.png"];
    }

     view.canShowCallout = YES;

     UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
     [rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
     view.rightCalloutAccessoryView = rightButton;

     UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"your_icon_image"]];
     view.leftCalloutAccessoryView = icon;

     return view;
    }

然后你可以打电话:

-(void)showDetails:(UIButton*)button
{
   //do your stuff here
}

完全没有必要自己构建注释标注视图,只需更改默认标注的设计。