1

我有一个地图注释,我想在calloutAccessoryControlTapped触摸时将其推送到详细视图控制器。我遇到的问题是所有注释都将相同的数据发送到详细信息 vc。我不确定是否是 indexPath 不正确。每个地图注释在标注框中显示正确的信息,只是当calloutAccessoryControlTapped点击它时,它会为数组中的第一个列表推送相同的信息。

这是 NSNumber *catListingMapId; 在我真正需要访问并传递给详细 vc 的注释中(我在那里下载基于该 catListingMapId 的数据)。

- (void)mapView:(MKMapView *)mv annotationView:(MKAnnotationView *)pin calloutAccessoryControlTapped:(UIControl *)control {

    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

    MyAnnotation *theAnnotation = (MyAnnotation *) pin.annotation;

    NSLog(@"the Annotation %@",theAnnotation.catListingMapId);

    detailViewController.listingId = theAnnotation.catListingMapId;
  //  detailViewController.listingId = [[self.listingNodesArray objectAtIndex:selectedIndexPath] objectForKey:@"id"];

    NSNumber *catNumber = [NSNumber numberWithInt:[catID intValue]];

    detailViewController.catId = catNumber;

    NSLog(@"detailViewController.listingId  %@",detailViewController.listingId );
    [self.navigationController pushViewController:detailViewController animated:YES];
}

这是我的 myAntoationMap 文件:

@interface MyAnnotation : NSObject<MKAnnotation> {

CLLocationCoordinate2D  coordinate;
NSString*               title;
NSString*               subtitle;
NSNumber *latString;
NSNumber *lngString;

NSNumber *catMapId;
NSNumber *catListingMapId;

}

@property (nonatomic, assign)   CLLocationCoordinate2D  coordinate;
@property (nonatomic, copy)     NSString*               title;
@property (nonatomic, copy)     NSString*               subtitle;

@property (nonatomic,copy) NSNumber *latString;
@property (nonatomic,copy) NSNumber *lngString;
@property (nonatomic,copy) NSNumber *catMapId;
@property (nonatomic,copy) NSNumber *catListingMapId;

@end


and the .m

    #import "MyAnnotation.h"

@implementation MyAnnotation

@synthesize title;
@synthesize subtitle;
@synthesize coordinate;
@synthesize latString,lngString;
@synthesize catMapId;
@synthesize catListingMapId;

- (void)dealloc 
{
    [super dealloc];
    self.title = nil;
    self.subtitle = nil;
    self.latString = nil;
    self.lngString = nil;
    self.catMapId = nil;
    self.catListingMapId = nil;
}
@end
4

3 回答 3

6

MKMapViewDelegate 具有以下委托方法,每次用户点击 calloutAccessory 按钮时都会调用该方法,因此这里我们获取注释标题和副标题,通过使用这些值,您可以将数据传递给下一个 viewController

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"%@",view.annotation.title);
    NSLog(@"%@",view.annotation.subtitle);
}
于 2013-09-10T09:36:51.177 回答
1
detailViewController.listingId = theAnnotation.catListingMapId

并在 detailViewController 中通过 listID 找到其他详细信息

编辑:

@interface MyAnnotation

写一个方法:

- (NSDictionary *) getTheAnnotationData{
    NSDictionary* theDict = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSString stringWithFormat:@"%f",latString],@"latString",
                             [NSString stringWithFormat:@"%f",lngString],@"lngString",
                             [NSString stringWithFormat:@"%i",catMapId],@"catMapId",
                             [NSString stringWithFormat:@"%i",catListingMapId],@"catListingMapId",
                             title,@"title",
                             subtitle,@"subtitle",
                             nil];

    return theDict;
}

此方法将为您提供字典中注释中的所有数据。

也将其写入 .h :

- (NSDictionary *) getTheAnnotationData;

然后:

DetailViewController:

@property (nonatomic, retain) NSDictionary* detailDictionary;

然后:

在你的calloutAccessoryControlTapped方法中:

- (void)mapView:(MKMapView *)mv annotationView:(MKAnnotationView *)pin calloutAccessoryControlTapped:(UIControl *)control {

    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

    MyAnnotation *theAnnotation = (MyAnnotation *) pin.annotation;

    NSLog(@"the Annotation %@",theAnnotation.catListingMapId);

    detailViewController.detailDictionary = [theAnnotation getTheAnnotationData];

    NSLog(@"detailViewController.listingId  %@",detailViewController.listingId );
    [self.navigationController pushViewController:detailViewController animated:YES];
}

然后详细ViewController:

@ synthesize detailDictionary

并在viewDidload

你可以得到你想要的关于你的注释的所有数据

我希望它有帮助

于 2013-09-09T22:27:29.437 回答
1

所选行的索引 ( [self.tableView indexPathForSelectedRow]) 与某人在地图上选择的注释有什么关系?又catID从何而来?

如果您想在 上显示detailViewController与某人刚刚按下的注释相关的内容,那么您需要使用来自 的信息pin,目前您正在使用两个不是来自该引脚的数据点,并且可能每次都相同,所以这就是你在detailViewController.

于 2013-09-09T22:53:39.630 回答