I've a problem with Annotation when clicked and send data to "Prepare to segue"
I've used this code:
- (void)viewDidLoad {
[super viewDidLoad];
PFQuery *query = [PFQuery queryWithClassName:@"Ristoranti"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error)
{
NSLog(@"QUERY -----> :%@", objects);
for(NSMutableDictionary *note1 in objects) {
float realLatitude1 = [[note1 objectForKey:@"Latitudine"] floatValue];
float realLongitude1 = [[note1 objectForKey:@"Longitudine"] floatValue];
NSLog(@"(PARSE) Latitudine: %f", realLatitude1);
NSLog(@"(PARSE) Longitudine: %f", realLongitude1);
DisplayMap *displayMap = [[DisplayMap alloc] init];
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = realLatitude1;
theCoordinate.longitude = realLongitude1;
displayMap.coordinate = theCoordinate;
displayMap.title = [note1 objectForKey:@"NomeLocale"];
displayMap.subtitle = [note1 objectForKey:@"Indirizzo"];
displayMap.icon = [note1 objectForKey:@"PinMappa"];
[_mapView setDelegate:self];
[_mapView addAnnotation:displayMap];
}
}
}];
}
and:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if (![annotation isKindOfClass:[DisplayMap class]])
{
return nil;
}
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKAnnotationView *pinView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (pinView == nil)
{
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
pinView.canShowCallout = YES;
}
else
pinView.annotation = annotation;
DisplayMap *myAnn = (DisplayMap *)annotation;
pinView.image = [UIImage imageNamed:myAnn.icon];
// Create a UIButton object to add on the
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[pinView setRightCalloutAccessoryView:rightButton];
return pinView;
}
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
if ([(UIButton*)control buttonType] == UIButtonTypeDetailDisclosure)
{
NSLog(@"The annotation tapped is: %@", view.annotation.title);
NSUInteger index = [mapView.annotations indexOfObject:view.annotation];
NSLog(@"%lu", (unsigned long)index);
//[self performSegueWithIdentifier:@"Prova" sender:self];
}
}
and:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"Prova"]) {
DettagliAlberghi *destViewController = segue.destinationViewController;
DisplayMap *displayMap = [[DisplayMap alloc] init];
displayMap.title = [note1 objectForKey:@"NomeLocale"];
displayMap.subtitle = [note1 objectForKey:@"Indirizzo"];
displayMap.icon = [note1 objectForKey:@"PinMappa"];
displayMap.Telefono = [note1 objectForKey:@"Telefono"];
displayMap.Email = [note1 objectForKey:@"Email"];
displayMap.Sito = [note1 objectForKey:@"Sito"];
displayMap.TipologiaLocale = [note1 objectForKey:@"TipologiaLocale"];
displayMap.Cucina = [note1 objectForKey:@"Cucina"];
displayMap.Vegano = [note1 objectForKey:@"Vegano"];
displayMap.Valutazione = [note1 objectForKey:@"Valutazione"];
displayMap.Latitudine = [note1 objectForKey:@"Latitudine"];
displayMap.Longitudine = [note1 objectForKey:@"Longitudine"];
displayMap.Anteprima1 = [note1 objectForKey:@"Anteprima1"];
displayMap.Anteprima2 = [note1 objectForKey:@"Anteprima2"];
displayMap.Anteprima3 = [note1 objectForKey:@"Anteprima3"];
displayMap.FaceB = [note1 objectForKey:@"Facebook"];
displayMap.Twit = [note1 objectForKey:@"Twitter"];
NSLog(@"Dettagli Ristorante: \n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@", displayMap.title, displayMap.subtitle, displayMap.Telefono, displayMap.Email, displayMap.Sito, displayMap.TipologiaLocale, displayMap.Cucina, displayMap.Vegano, displayMap.Valutazione, displayMap.Latitudine, displayMap.Longitudine, displayMap.Anteprima1, displayMap.Anteprima2, displayMap.Anteprima3, displayMap.FaceB, displayMap.Twit);
destViewController.recipes = displayMap;
}
}
I would like to understand how to recognize some annotation is pressed and send the data to the next page through "PrepareForSegue". Does anyone know how to help me? I'm going crazy. Thanks in advance.
You will simply want to use the mapView:didSelectAnnotationView: delegate call.
https://developer.apple.com/documentation/mapkit/mkmapviewdelegate/1452393-mapview
From in mapView:didSelectAnnotationView, call the segue call via the performSegueWithIdentifier.
Assuming you setup "note1" from within mapView:didSelectAnnotationView based on the annotation selected.