Search code examples
iosobjective-cxcodemkmapviewmapkit

Xcode/Objective-C - pass data through prepareForSegue


I have a UIViewController with a MapView. My prepareForSegue Method looks like this:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"mapDetailPage"]) {
        vumLocationViewController *vvc = segue.destinationViewController;

    vvc.profilId = [[self.parser.locationsArray objectAtIndex:1] profilId];
    vvc.profilType = [[self.parser.locationsArray objectAtIndex:1] profilType];
    vvc.displayName = [[self.parser.locationsArray objectAtIndex:1] displayName];

    return;
    }
}

Now the Problem is I need an IndexPath(or Sth. like that) to get the Data from the right index and not from the index 1.

Here are the other maybe important lines:

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    if ((oldLocation.coordinate.longitude != newLocation.coordinate.longitude) || (oldLocation.coordinate.latitude != newLocation.coordinate.latitude))
    {

        CLLocationCoordinate2D coord = {
            .latitude = newLocation.coordinate.latitude,
            .longitude = newLocation.coordinate.longitude};

        MKCoordinateRegion region;
        region.center = coord;

        MKCoordinateSpan span = {.latitudeDelta = 0.1, .longitudeDelta = 0.1};
        region.span = span;

        for(int i = 0; i < [self.parser.locationsArray count]; i++)
        {
            double latitude = [[[self.parser.locationsArray objectAtIndex:i] latitude] doubleValue];
            double longitude = [[[self.parser.locationsArray objectAtIndex:i] longitude] doubleValue];
            coord.latitude = latitude;
            coord.longitude = longitude;


            NSString *openingTimes = [NSString stringWithFormat:@"%@\n%@\n%@", [[self.parser.locationsArray objectAtIndex:i] street] ,[[self.parser.locationsArray objectAtIndex:i] opening1], [[self.parser.locationsArray objectAtIndex:i] opening2]];


            PlaceMark *placeMark = [[PlaceMark alloc]
                                    initWithCoordinate:coord
                                    andMarkTitle: [[self.parser.locationsArray objectAtIndex:i] displayName]
                                    andMarkSubTitle:openingTimes
                                    andMarkProfilType:[[self.parser.locationsArray objectAtIndex:i] profilType]];
            [myMapView addAnnotation:placeMark];
            placeMark = nil;
        }

    }
}

    //view For Annotation
    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {

    static NSString *identifier = @"myLocation";
    if ([annotation isKindOfClass:[PlaceMark class]])
    {
        MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annotationView == nil)
        {
            annotationView = [[MKPinAnnotationView alloc]
                              initWithAnnotation:annotation
                              reuseIdentifier:identifier];
        }
        else
        {
            annotationView.annotation = annotation;
        }

        annotationView.enabled = YES;
        annotationView.canShowCallout = YES;
        UIImageView *calloutImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gastronomie.png"]];
        annotationView.leftCalloutAccessoryView = calloutImage;
        annotationView.highlighted = YES;

        /*if([[[self.parser.locationsArray objectAtIndex: annotation] category] isEqualToString:@"Gastronomie"])
        {
            NSLog(@"Ist Gastronomie");
            annotationView.pinColor = MKPinAnnotationColorRed;
        }*/

        annotationView.pinColor = MKPinAnnotationColorRed;

        // Create a UIButton object to add on the
        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [rightButton setTitle:annotation.title forState:UIControlStateNormal];
        [annotationView setRightCalloutAccessoryView:rightButton];

        return annotationView;
    }
    return nil;
}


    //calloutAccessoryControlTapped
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {

    if ([(UIButton*)control buttonType] == UIButtonTypeDetailDisclosure)
    {
        if (![view.annotation isKindOfClass:[PlaceMark class]])
            return;

        // use the annotation view as the sender
        [self performSegueWithIdentifier:@"mapDetailPage" sender: view];

        // Do your thing when the detailDisclosureButton is touched

        UIViewController *vumLocationViewController = [[UIViewController alloc] init];
        [[self navigationController] pushViewController:vumLocationViewController animated:YES];

    }
}

So how do I get this Array Index in the prepareForSegue method?


Solution

  • You can pass everything you need via sender parameter.
    Look, in your current implementation you are passing MKAnnotationView instance in [self performSegueWithIdentifier:@"mapDetailPage" sender: view];. So, in your -(void)prepareForSegue:sender:. You can get an annotation - PlaceMark *annotation = (PlaceMark*)[(MKAnnotationView*)sender annotation];
    From that annotation you can get anything you set to it, like profilType, latitude etc. Disclosing this to full code:

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([segue.identifier isEqualToString:@"mapDetailPage"]) {
            vumLocationViewController *vvc = segue.destinationViewController;
    
        PlaceMark *annotation = (PlaceMark*)[(MKAnnotationView*)sender annotation];
        vvc.profilId = [annotation profilId];
        vvc.profilType = [annotation profilType];
        vvc.displayName = [annotation displayName];
        }
    }