2013-05-29 154 views
1

我有一个引脚在地图上显示来自数据库的JSON字符串的数据。我设置了引脚的ID(在自定义类中定义),但是当按下详细信息泄漏按钮时,我需要将ID传递给一个操作。详细信息披露发送自定义注释数据

然后它会打开与该引脚上的一些信息,一个新的视图(通过一个数据库,它会查找该行ID和返回数据,不知道IM如何去做到这一点,只是还没有)

这是代码:

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

} 

回答

3

在我看来,你希望检测哪个注释被选中或者它的辅助视图被点击。

使用下面的方法

-(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

+0

嗨!感谢你,我appriciate它。第一个方法是否替换上面代码中的viewForAnnotation方法?感谢您的链接,我会看看那里的项目 - 看起来像一个伟大的网站! –

+0

如果你在我的文章中谈到第一种方法,那么它不会取代viewforannotation。 viewForAnnotation是为了不同的目的,calloutAccessoryControlTapped是检测附件控件,在你的mkannotationview的标注上,当你点击标注本身时出现。请通过链接建议,肯定会帮助您更好地理解MKMapView及其代表的使用逻辑和用法。 –

相关问题