2015-04-01 47 views
0

您好我有自定義視圖在didSelectAnnotationView當用戶點擊製造商我顯示與UILabel和UIButton自定義視圖一切都工作正常,但UIButton不可點擊請告訴如何解決這個問題。UIButton無法正常工作didSelectAnnotationView

我的代碼。

 -(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"); 
    } 

我已經使用上面的代碼,它不工作,請告訴我在哪裏做錯了如何解決這個問題。

在此先感謝。

回答

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 
} 

完全沒有必要建立一個註釋標註看待自己,只是改變了默認的設計之一。

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]; 


} 

檢查這個代碼

+0

我改變了它仍然不能正常工作 – Shanthanu 2015-04-01 06:50:01

+0

什麼是測試? – 2015-04-01 07:11:07