MapView向自定义注释添加操作(右箭头)

时间:2013-05-24 12:04:45

标签: ios objective-c cocoa-touch

我通过这个for循环创建注释引脚:

UIButton *eventMore = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

for (i = 0; i < [results count]; i++) {
    //NSLog(@"Result: %i = %@", i, results[i]);
    //NSLog(@"%@",[[results objectAtIndex:i] objectForKey:@"long"]);

    myAnn = [[CustomAnnotation alloc] init];
    location.latitude = (double)[[[results objectAtIndex:i] objectForKey:@"lat"] doubleValue];
    location.longitude = (double)[[[results objectAtIndex:i] objectForKey:@"long"] doubleValue];
    myAnn.coordinate = location;
    myAnn.title = [[results objectAtIndex:i] objectForKey:@"title"];
    myAnn.subtitle = [[results objectAtIndex:i] objectForKey:@"strap"];
    [eventMore addTarget:myAnn action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [locations addObject:myAnn];

    //NSLog(@"%i", [[results objectAtIndex:i] objectForKey:@"lat"]);
}

我想要做的是在注释的标题/描述的右侧添加详细信息公开按钮但是这不会添加一个,并且我找到的所有这些都使用此方法做到这一点 - 但它没有用,有什么建议吗?

行动代码:

-(void)btnClicked:(id)sender{
    UIAlertView *btnAlert = [[UIAlertView alloc] initWithTitle:@"title" message:@"msg" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil];
    [btnAlert show];
}

1 个答案:

答案 0 :(得分:3)

您没有为注释设置 CalloutAccessoryView

示例代码

    for (i = 0; i < [results count]; i++) {

    MKPointAnnotation  *myAnn = [[MKPointAnnotation alloc] init];
    location.latitude = (double)[[[results objectAtIndex:i] objectForKey:@"lat"] doubleValue];
    location.longitude = (double)[[[results objectAtIndex:i] objectForKey:@"long"] doubleValue];
    myAnn.coordinate = location;
    myAnn.title = [[results objectAtIndex:i] objectForKey:@"title"];
    myAnn.subtitle = [[results objectAtIndex:i] objectForKey:@"strap"];
    [locations addObject:myAnn];

   }


   - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{

   static NSString *s = @"ann";
   MKAnnotationView *pin = [mapView dequeueReusableAnnotationViewWithIdentifier:s];
   if (!pin) {
    pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:s];
    pin.canShowCallout = YES;
    pin.image = [UIImage imageNamed:@"pin.png"];

    pin.calloutOffset = CGPointMake(0, 0);
    UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [button addTarget:self
               action:@selector(viewDetails) forControlEvents:UIControlEventTouchUpInside];
    pin.rightCalloutAccessoryView = button;

}
return pin;
}