gpt4 book ai didi

ios - Xcode/Objective-C - 通过prepareForSegue传递数据

转载 作者:行者123 更新时间:2023-11-29 02:43:13 24 4
gpt4 key购买 nike

我有一个带有 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 方法中获取这个 Array Index 呢?

最佳答案

您可以通过 sender 参数传递您需要的所有内容。
看,在当前的实现中,您正在 [self PerformSegueWithIdentifier:@"mapDetailPage"sender: view]; 中传递 MKAnnotationView 实例。因此,在您的 -(void)prepareForSegue:sender: 中。您可以获得注释 - PlaceMark *annotation = (PlaceMark*)[(MKAnnotationView*)sender comment];
从该注释中,您可以获取您设置的任何内容,例如 profilType、纬度等。将其公开给完整代码:

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

关于ios - Xcode/Objective-C - 通过prepareForSegue传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25482922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com