正确触摸如何使用`calloutAccessoryControlTapped`将数据从一个视图控制器发送到详细视图控制器

时间:2013-09-09 22:18:12

标签: iphone ios objective-c mkmapview

我有一个地图注释,我想在触摸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];
}

这是我的myAnntoationMap文件:

@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

3 个答案:

答案 0 :(得分:6)

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

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"%@",view.annotation.title);
    NSLog(@"%@",view.annotation.subtitle);
}

答案 1 :(得分:1)

detailViewController.listingId = theAnnotation.catListingMapId

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

编辑:

in

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

然后在detailViewController中:

@ synthesize detailDictionary

viewDidload

您可以获得有关注释的所有数据

我希望它有所帮助

答案 2 :(得分:1)

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

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