【问题标题】:how send Core Data object from MapView to DetailView如何将核心数据对象从 MapView 发送到 DetailView
【发布时间】:2015-04-22 07:00:42
【问题描述】:

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

-(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 视图并发送完整的健身房对象 (KMTInstalacion) 对应您按下的 pinView,而不仅仅是标题或副标题,因为每个健身房大约有 50 个属性(照片、时间表等...)

知道怎么做吗?

【问题讨论】:

    标签: ios objective-c core-data mkmapview mkannotation


    【解决方案1】:

    在您的DetailViewController.h 文件中创建一个健身房对象的属性。同时创建一个具有相同属性的自定义注解(MKAnnotation 的子类)。

    @property (nonatomic, strong) KMTInstalacion *gymObj;
    

    设置注释时,将gymObj 分配给它的属性。 然后在你的 Map Controllers .m 文件中实现这个 MKMapView 的委托方法:

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

    【讨论】:

    • 是的,这是我建议的方法。 GymObject 是指 OP 的 KMTInstalacion 类。关键部分是“创建自定义注释类”(而不是使用 MKPointAnnotation)。
    • 我试过了,但它不起作用。我用这段代码创建了我的课程:#import &lt;MapKit/MapKit.h&gt; #import "KMTInstalacion.h" @interface KMTMiAnotacion : NSObject &lt;MKAnnotation&gt;{ NSString *title; NSString *subTitle; KMTInstalacion *instalacion; } @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *subTitle; @property (nonatomic, copy) KMTInstalacion *instalacion; 但没有用,我收到了一个错误
    • 2015-04-22 11:20:13.280 DonostiaKirola[8078:2448702] -[MKPointAnnotation instalacion]: unrecognized selector sent to instance 0x1742572e0 2015-04-22 11:20:13.282 DonostiaKirola[8078:2448702] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MKPointAnnotation instalacion]: unrecognized selector sent to instance 0x1742572e0'
    • 您是如何尝试设置和获取instalacion 对象的?
    • 我自己做 Annotatin 类 y 我有树属性“Title”、“Subtitle”和 KMTInstalacion *instalacion。然后我尝试在 calloutAccessoryControlTapped 中传递这段代码:KMTMianotacion *anotacion = view.annotation; KMTInformacion *detailView = [[KMTInformacion alloc]init]; detailView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; detailView.instalacionRecibida = annView.instalacion; [self performSegueWithIdentifier:@"deMapa" sender:self];
    【解决方案2】:

    您可以使用下面的代码示例如何将详细信息发送到其他视图。根据您的需要,您可以将整个对象发送到其他视图...!

    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
     calloutAccessoryControlTapped:(UIControl *)control
    {
    annotation *annView = view.annotation;
    DetailedViewController *detailVC = [[DetailedViewController alloc] initWithNibName:@"DetailedViewController" bundle:nil];
    detailVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    detailVC.address = annView.address;
    detailVC.name = annView.name;
    [self presentModalViewController:detailVC animated:YES];
    }
    

    希望对你有帮助..

    【讨论】:

      【解决方案3】:

      首先在您的注释视图委托中制作一个按钮以进入详细视图,如下所示:

      -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
      
      MKPinAnnotationView *mypin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"current"];
      mypin.pinColor = MKPinAnnotationColorPurple;
      mypin.backgroundColor = [UIColor clearColor];
      UIButton *goToDetail = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
      mypin.rightCalloutAccessoryView = myBtn;
      mypin.draggable = NO;
      mypin.highlighted = YES;
      mypin.animatesDrop = TRUE;
      mypin.canShowCallout = YES;
      return mypin;
      }
      

      现在,只要点击 annotationView 中的按钮,就使用以下委托,然后将调用以下委托,您可以轻松获取点击了哪个特定注释的按钮

      - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
       calloutAccessoryControlTapped:(UIControl *)control
      {
      annotation *annView = view.annotation;
      detailedViewOfList *detailView = [[detailedViewOfList alloc]init];
      detailView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
      detailView.address = annView.address;
      detailView.phoneNumber = annView.phonenumber;
      [self presentModalViewController:detailView animated:YES];
      

      [self.navigationController pushViewController:detailView animated:YES];
          }
      

      这里的annotaion 是一个导入MKAnnotaion.h 的类,address 和phonenumber 是annotaion 类的属性,你可以做更多,而detailView 类的address 和phoneNumber 属性很强大。这样您就可以传递值。希望对您有所帮助!

      【讨论】:

      • 它不起作用。问题是因为我从 KMTInstalacion 对象中提取了标题和副标题,我需要整个传递这个对象。错误是:2015-04-22 11:20:13.280 DonostiaKirola[8078:2448702] -[MKPointAnnotation instalacion]: unrecognized selector sent to instance 0x1742572e0 2015-04-22 11:20:13.282 DonostiaKirola[8078:2448702] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MKPointAnnotation instalacion]: unrecognized selector sent to instance 0x1742572e0'
      猜你喜欢
      • 2013-10-27
      • 2015-04-10
      • 2019-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多