ios - UIButton 不适用于 didSelectAnnotationView

标签 ios uibutton mkannotationview

您好,当用户点击图钉制作器时,我在 didSelectAnnotationView 中有自定义 View 我正在显示带有 UILabel 和 UIButton 的自定义 View ,一切正常,但 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");
   }

我已经使用了上面的代码,它不起作用请告诉我我哪里做错了如何解决这个问题。

提前致谢。

最佳答案

它不起作用,因为 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
}

完全没有必要自己构建annotation callout view,只需更改默认的设计即可。

关于ios - UIButton 不适用于 didSelectAnnotationView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29384082/

相关文章:

ios - 通知 iOS 用户必须打开蓝牙

ios - 是否有可能在 iOS 代码中获取按钮单击的确切位置(快速)

ios - UIButton,UIAlertView 设置 Action

ios - 为什么从自定义 UIView 向 UIButton 添加 subview 不显示添加的 subview ?

cocoa-touch - 如何为 MKAnnotationView 放置动画?

ios - Swift 中如何判断 MKPointAnnotation 是否被按下?

ios - 我可以禁用单个单元格的 UITableView 选择吗?

objective-c - 从详细 View 返回后, TableView 不再是 searchResultsTableView

ios - 自定义 UIbutton 默认颜色 Xcode Swift

iphone - 标注气泡MKMapView移动问题