如何在按下地图视图注释时使用segue在详细视图中显示plist中的数据

时间:2013-09-05 14:35:35

标签: ios objective-c mapkit mapkitannotation

因为标题描述我无法在地图视图项目中显示我在plist中用于注释的数据,我正在使用故事板中的segue。我有地图视图,所有注释(引脚)正在显示他们的坐标。现在我想在用户按下详细信息披露按钮时使用segue转换到详细视图。到目前为止,我有以下这是一个空表演segue:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
[self performSegueWithIdentifier:@"Details" sender:view];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
}

我想在详细视图中显示的细节被声明为字符串。如下

@interface ItalyMapDetail : UIViewController{
IBOutlet UIImageView *appImage;
IBOutlet UITextView *appText;
IBOutlet UILabel *appName;

NSString *appImageString;
NSString *appTextString;
NSString *appNameString;

}

@property (nonatomic, retain) NSString *appImageString;
@property (nonatomic, retain) NSString *appTextString;
@property (nonatomic, retain) NSString *appNameString;
@property (nonatomic, strong) UIImage* image;

@end

并在.m文件中如下

@synthesize appImageString, appTextString, appNameString;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

appImage.image = [UIImage imageNamed:appImageString];
appText.text = appTextString;
appName.text = appNameString;
}

这是详细视图中某些字符串的真正通用声明。由于我已经有一个导航控制器,因此segue标识符将触发并且视图将转换为详细视图并正确返回。现在我知道如何在tableview中声明字符串准备segue但我不知道如何从segue中的plist声明这些细节,以便在详细视图控制器中显示注释细节。任何人都可以帮我一把。

哦顺便说一句,在我看来,我从plist派生出来的注释的删除如果有帮助就会加载。

NSMutableArray *annotations = [[NSMutableArray alloc]init];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Map" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSArray *anns = [dict objectForKey:@"Italy"];

for(int i = 0; i < [anns count]; i++) {
    float realLatitude = [[[anns objectAtIndex:i] objectForKey:@"Latitude"] floatValue];
    float realLongitude = [[[anns objectAtIndex:i] objectForKey:@"Longitude"] floatValue];

    Annotation *myAnnotation = [[Annotation alloc] init];
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = realLatitude;
    theCoordinate.longitude = realLongitude;
    myAnnotation.coordinate = theCoordinate;
    myAnnotation.title = [[anns objectAtIndex:i] objectForKey:@"Title"];
    myAnnotation.subtitle = [[anns objectAtIndex:i] objectForKey:@"Subtitle"];
    [myMapView addAnnotation:myAnnotation];
    [annotations addObject:myAnnotation];
}

我知道这对你们大多数人来说可能很容易,但我真的很困难,我可以在代码级别使用一些帮助。

编辑1:

这是建议更改后的代码:

    NSMutableArray *annotations = [[NSMutableArray alloc]init];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"ItalyMap" ofType:@"plist"];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
    NSArray *anns = [dict objectForKey:@"Italy"];

    for(int i = 0; i < [anns count]; i++) {
    float realLatitude = [[[anns objectAtIndex:i] objectForKey:@"Latitude"] floatValue];
    float realLongitude = [[[anns objectAtIndex:i] objectForKey:@"Longitude"] floatValue];

    Annotation *myAnnotation = [[Annotation alloc] init];
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = realLatitude;
    theCoordinate.longitude = realLongitude;
    myAnnotation.coordinate = theCoordinate;
    myAnnotation.title = [[anns objectAtIndex:i] objectForKey:@"Title"];
    myAnnotation.subtitle = [[anns objectAtIndex:i] objectForKey:@"Subtitle"];
    [myMapView addAnnotation:myAnnotation];
    [annotations addObject:myAnnotation];

    myAnnotation.annotationData = [anns objectAtIndex:i];
}

}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(@"view for annotation works");

if([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

static NSString *annotationIdentifier = @"AnnotationIdentifier";

MKPinAnnotationView *pinView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];

if (!pinView)
{
    pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];

    [pinView setPinColor:MKPinAnnotationColorGreen];
    pinView.animatesDrop = YES;
    pinView.canShowCallout = YES;

    UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"acolon1.png"]];
    pinView.leftCalloutAccessoryView = iconView;

    pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
else
{
    pinView.annotation = annotation;
}

return pinView;
}

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
// Keep a reference to the callout's annotation data
self.selectedAnnotationData = [(Annotation *)view.annotation annotationData];

[self performSegueWithIdentifier:@"Details" sender:self];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"Details"])
{
    ItalyMapDetail *controller = segue.destinationViewController;
    controller.annotationData = self.selectedAnnotationData;
}
}

现在我已将dic添加到注释中并导入详细视图。唯一没有显示的是细节视图中的字符串和图像。

1 个答案:

答案 0 :(得分:1)

您可以使用

将特定数据对象传递给Annotation
myAnnotation.annotationData = [anns objectAtIndex:i];

然后在prepareForSegue:sender:中,您可以访问注释数据并将其传递给详细视图控制器。


修改:添加了示例代码

在Annotation类上声明一个属性:

@property (nonatomic, strong) NSDictionary *annotationData;

在与地图视图相同的视图控制器上声明属性:

@property (nonatomic, strong) NSDictionary *selectedAnnotationData;

实现地图视图委托方法:

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
    // Keep a reference to the callout's annotation data
    self.selectedAnnotationData = [(Annotation *)view.annotation annotationData];

    [self performSegueWithIdentifier:@"MY_SEGUE_IDENTIFIER" sender:self];
}

然后将注释数据传递给prepareForSegue:sender:method:

中的后续控制器
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"showPlattegrondDetails"])
    {
        ItalyMapDetail *controller = segue.destinationViewController;
        controller.annotationData = self.selectedAnnotationData;
    }
}

编辑2:根据已修改的问题。

在不知道plist中键的名称的情况下,我只能指出你应该看的方向。

您的ItalyMapDetail控制器应该具有前一个控制器的prepareForSegue:sender:中提供的注释数据字典,它应该只是使用该字典中的值来填充您的UI ...

更新viewDidLoad控制器的ItalyMapDetail方法,如下所示:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // temporary test: print out the annotation data so
    // we can see what we've got. This shouldn't be nil.
    NSLog("Annotation Data: %@", self.annotationData);

    // replace these keys with appropriate ones for your plist
    appImage.image = [UIImage imageNamed:self.annotationData[@"imageName"]];
    appText.text = self.annotationData[@"appText"];
    appName.text = self.annotationData[@"appName"];
}