0

I have a list of gyms with all its properties in Core Data, I get this gyms with an NSFetchRequest and pass its to a NSArray, then with a For in and this array, I get to put the pins in the MapView, I do it with de next code (welcome suggestions for improvement this):

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

When I tap on one of the pinView shows me the name and address of the gym with the button to the right.

Now what I want is when you tap on the pinView button, take you to the DetailView view and send full gym object (KMTInstalacion) corresponding pinView you've pressed, not just the title or subtitle, since each gym has about 50 properties (photos, schedules, etc ...)

Any idea how to do this?

3 Answers3

1

Make a property of the gym object in your DetailViewController's .h file. Also create a custom annotation (subclass of MKAnnotation) with the same property.

@property (nonatomic, strong) KMTInstalacion *gymObj;

When setting the annotation, assign the gymObj to it's property. Then implement this delegate method of MKMapView in your Map Controllers .m file:

- (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
}
Shabib
  • 1,697
  • 4
  • 20
  • 39
  • 1
    Yes, this is the approach I would suggest. By `GymObject`, you mean the OP's `KMTInstalacion` class. The key part is to "create a custom annotation class" (instead of using MKPointAnnotation). –  Apr 22 '15 at 11:27
  • I tried this and it does not work. I created my class with this code: `#import #import "KMTInstalacion.h" @interface KMTMiAnotacion : NSObject { NSString *title; NSString *subTitle; KMTInstalacion *instalacion; } @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *subTitle; @property (nonatomic, copy) KMTInstalacion *instalacion;` but didn't work, I received an error – Aitor Uranga Barandika Apr 23 '15 at 06:52
  • `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' ` – Aitor Uranga Barandika Apr 23 '15 at 06:53
  • how have you tried to set and get the `instalacion` object? – Shabib Apr 23 '15 at 07:48
  • I do it my own Annotatin class y there I have tree properties "Title", "Subtitle" and KMTInstalacion *instalacion. Then I tried to pass whith this code in calloutAccessoryControlTapped: `KMTMianotacion *anotacion = view.annotation; KMTInformacion *detailView = [[KMTInformacion alloc]init]; detailView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; detailView.instalacionRecibida = annView.instalacion; [self performSegueWithIdentifier:@"deMapa" sender:self];` – Aitor Uranga Barandika Apr 23 '15 at 09:52
  • How did you create the custom annotation and set it's property? – Shabib Apr 23 '15 at 11:31
  • You're setting `KMTMiAnotacion *anotacion = view.annotation;`, but I don't see `KMTMiAnotacion` object being created (`alloc` & `init`) and being set it's `instalacion` object to the `instalacion` object from your array. Also, you're still instantiating the default point annotation and in the delegate method, the assignment to ``KMTMiAnotacion *anotacion` is the pain old `MKAnnotation` property of the `MKAnnotationView ` object. If you don't assign the `instalacion` object properly, this won't work. – Shabib Apr 24 '15 at 07:11
  • do not quite understand what I have to do ... I have to change `KMTMiAnotacion *anotacion = view.annotation;` by `KMTMiAnotacion *anotacion = [[KMTMiAnotacion alloc]init] anotacion = view.annotation` I do not know how to change the following you say. – Aitor Uranga Barandika Apr 27 '15 at 07:05
0

You can use below code for sample how u can send details to other view. As per your need you to send entire object to other view...!

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

Hope it helps you..

Vidhyanand
  • 5,369
  • 4
  • 26
  • 59
0

First in your annotaion view delegat make a button to go in detail view like bellow:

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

Now use the following delegate whenever the button in annotationView will get tapped the following delegate will be called from where you can easly get which particular annotaion's button is tapped

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

OR

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

here annotaion is a class importing MKAnnotaion.h and address and phonenumber are properties of annotaion class you can make many more while the address and phoneNumber properties of detailView class are strong. So that you can pass values. Hope this will help you!

Rahul Mayani
  • 3,761
  • 4
  • 25
  • 40
  • It does not work. The problem is because I extract the title and subtitle from KMTInstalacion object and I need pass this objetct entire. The error is: `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'` – Aitor Uranga Barandika Apr 22 '15 at 09:36
  • Refer this url: http://stackoverflow.com/questions/29548143/xcode-6-3-mkpointannotation-setcoordinate-missing – Rahul Mayani Apr 22 '15 at 10:03