gpt4 book ai didi

ios - 如何将核心数据对象从 MapView 发送到 DetailView

转载 作者:行者123 更新时间:2023-11-29 02:06:01 26 4
gpt4 key购买 nike

我在 Core Data 中有一个健身房列表及其所有属性,我使用 NSFetchRequest 获取这个健身房并将其传递给 NSArray,然后使用 For in 和这个数组,我将图钉放在 MapView 中,我用 de next 代码来做(欢迎提出改进建议):

-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];

NSFetchRequest *reqInst = [NSFetchRequest fetchRequestWithEntityName:[KMTInstalacion entityName]];
reqInst.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:KMTInstalacionAttributes.tipoInstalacion_ES ascending:NO]];

KMTAppDelegate *appDelegate = (KMTAppDelegate *)[[UIApplication sharedApplication]delegate];
NSError *error;
NSArray *arrayInstalaciones = [appDelegate.model.context executeFetchRequest:reqInst error:&error];

NSLog(@"Las instalaciones encontradas en arrayInstalaciones son: %lu", (unsigned long)[arrayInstalaciones count]);

for (KMTInstalacion *todasInstalaciones in arrayInstalaciones) {
CLLocationCoordinate2D coordPunto1 = CLLocationCoordinate2DMake(todasInstalaciones.coorLatitudValue, todasInstalaciones.coorLongitudValue);
MKPointAnnotation *anotacion = [[MKPointAnnotation alloc]init];
[anotacion setCoordinate:coordPunto1];
[anotacion setTitle:todasInstalaciones.nombre_ES];
[anotacion setSubtitle:todasInstalaciones.direccion_ES];
[self.mapView addAnnotation:anotacion];
}}

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"curr"];

pinView.animatesDrop = NO;
pinView.pinColor = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

return pinView;

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

KMTMiAnotacion *anotacion = view.annotation;
KMTDetailVC *detalle = [[KMTDetailVC allc]init];
detalle.title = anotacion.title
detalle.nombreInstalacionSeleccionada = anotacion.instalacion;

[self performSegueWithIdentifier:@"deMapa" sender:self];

}

KMTMiAnotacion.h

@interface KMTMiAnotacion : NSObject <MKAnnotation>{
NSString *title;
NSString *subTitle;
KMTInstalacion *instalacion;
}

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subTitle;
@property (nonatomic, copy) KMTInstalacion *instalacion;
@end

当我点击其中一个 pinView 时,右侧的按钮会显示健身房的名称和地址。

现在我想要的是当您点击 pinView 按钮时,将您带到 DetailView View 并发送完整的健身房对象 (KMTInstalacion) 对应您按下的 pinView,而不仅仅是标题或副标题,因为每个健身房都有大约 50属性(照片、时间表等...)

知道怎么做吗?

最佳答案

DetailViewController.h 文件中创建gym 对象的属性。还创建具有相同属性的自定义注释(MKAnnotation 的子类)。

@property (nonatomic, strong) KMTInstalacion *gymObj;

设置注释时,将gymObj分配给它的属性。然后在您的 map Controller .m 文件中实现 MKMapView 的委托(delegate)方法:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
calloutAccessoryControlTapped:(UIControl *)control
{
YourAnnotation *annView = view.annotation;
detailedViewOfList *detailView = [[DetailedViewOfList alloc]init];
detailView.gymObj = annotation.gymObj;
// push view modally or, however you want
}

关于ios - 如何将核心数据对象从 MapView 发送到 DetailView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29790052/

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