我在 map 上显示了来自数据库的 JSON 字符串数据的图钉。我设置了 pin 的 id(在自定义类中定义),但在按下详细信息披露按钮时,我需要将 ID 传递给操作。
然后它将打开一个新 View ,其中包含有关该引脚的一些信息(通过数据库,它将查找行 ID 并返回数据,尚不确定我将如何做到这一点)
这是代码:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[spinner stopAnimating];
// json parsing
results = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
NSMutableArray * locations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location;
CustomAnnotation * myAnn;
NSArray *pins = mapView.annotations;
if ([pins count])
{
[mapView removeAnnotations:pins];
}
/* loop through the array, each key in the array has a JSON String with format:
* title <- string
* strap <- string
* id <- int
* date <- date
* lat <- floating point double
* long <- floating point double
* link <- string
*/
int i;
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"];
myAnn.pinId = [[results objectAtIndex:i] objectForKey:@"id"];
NSLog(@"id: %i", myAnn.pinId);
[locations addObject:myAnn];
//NSLog(@"%i", [[results objectAtIndex:i] objectForKey:@"lat"]);
}
[self.mapView addAnnotations:locations];
}
- (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;
}
-(void)viewDetails{
NSLog(@"press");
}
最佳答案
在我看来,您希望检测选择了哪个注释或单击了它的附件 View 。
使用以下方法
-(void) mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
可用于检测标注点击操作。但不是设置按钮的标签,而是标记您将提供的用于显示注释的 mkannotationView
-(MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
一些最好的教程是
http://www.raywenderlich.com/21365/introduction-to-mapkit-in-ios-6-tutorial
关于ios - 细节披露从自定义注释发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16812329/