我有一个带有MapView的UIViewController。 我的prepareForSegue方法如下所示:
-(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;
}
}
现在的问题是我需要一个IndexPath(或类似的东西)来从正确的索引而不是从索引1获取数据。
以下是其他可能很重要的内容:
- (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];
}
}
那么如何在prepareForSegue方法中获取此数组索引?
答案 0 :(得分:1)
您可以通过sender
参数传递您需要的所有内容
看,在您当前的实现中,您正在MKAnnotationView
中传递[self performSegueWithIdentifier:@"mapDetailPage" sender: view];
实例。所以,在你的-(void)prepareForSegue:sender:
中。您可以获得注释 - PlaceMark *annotation = (PlaceMark*)[(MKAnnotationView*)sender annotation];
从该注释中,您可以获得您设置的任何内容,例如profilType,latitude等。将其公开为完整代码:
-(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];
}
}