0

I am new to xcode and i hope someone can help me with my code, and point me how to solve my issue. i've been looking at the internet, but really i couldnt figure it out.

How can i go from a map view to a detail view, bringing the "ID" of the pin with it and print it in a label. when i click on the arrow for the annotation, my simulator crashes.

Please any ideas, how to approach it and solve it ?

( basicly i want to get the ID of the pin, and send it to a server through JSON and then move to the Detailview, but the next step )

Here is what i did so far :

My Location.m ( view controller for the map )

#import "My_Location.h"
#import "DetailViewController.h"

// ------ FUNCTION TO GET COORDINATES IN JSON FORMAT -------

- (void)retrieveData_Location
{

    NSURL * url = [NSURL URLWithString:@"............./Pharmacies.php"];
    NSData * data = [NSData dataWithContentsOfURL:url];
    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSMutableArray *newAnnotations = [NSMutableArray array];

    CLLocationCoordinate2D location;

    for (NSDictionary *dictionary in array)
    {
    MKPointAnnotation *newAnnotation = [[MKPointAnnotation alloc] init];

    location.latitude = [dictionary[@"lat"] doubleValue];
    location.longitude = [dictionary[@"lon"] doubleValue];   
    newAnnotation.title = dictionary[@"name"];
    newAnnotation.coordinate = location;

    [newAnnotations addObject:newAnnotation];

    [newAnnotation release];      
}
    [self.MyLocation addAnnotations:newAnnotations];   
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {

if([annotation isKindOfClass:[MKUserLocation class]])
    return nil; 
static NSString *identifier = @"myAnnotation";
MKPinAnnotationView * annotationView = (MKPinAnnotationView*)[self.MyLocation    dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!annotationView)
{
    annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    annotationView.pinColor = MKPinAnnotationColorPurple;
    annotationView.animatesDrop = YES;
    annotationView.canShowCallout = YES;
}else {
    annotationView.annotation = annotation;
}
    annotationView.rightCalloutAccessoryView = [UIButton   buttonWithType:UIButtonTypeDetailDisclosure];
    return annotationView;
}


- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view   calloutAccessoryControlTapped:(UIControl *)control
{
    [self performSegueWithIdentifier:@"Details" sender:view];

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"Details"])
    {
        MKAnnotationView *annotationView = sender;
        [segue.destinationViewController setAnnotation:annotationView.annotation];
    }
}

Take_Photo is my detailviewcntroller.

And my storybaord look like this :

Mylocation View controller -> ( Segue: identifier ( Take_Photo )) -> Take_Photo view Controller

mounim
  • 3
  • 5
  • Describe what the server has to do with it? – trojanfoe Sep 02 '14 at 12:51
  • 2
    And what is the crash message? – Paulw11 Sep 02 '14 at 12:54
  • The idea, is that the user can choose the closest store to him, then take a picture of a document, and then send it to a Mysql database ( with a store id). on the other side, the "choosed store" can log in through a website a see the sendt document. so i need to send the (ID) of the choosed store in the map, to the next detailview controler ( to take picture ) when this is done, i can parse the picture with the ID of the store through JSON.. i hope this make sens – mounim Sep 02 '14 at 14:05
  • See http://stackoverflow.com/questions/14805954/mkannotationview-push-to-view-controller-when-detaildesclosure-button-is-clicked –  Sep 02 '14 at 14:15
  • Please don't use a global variable. Even without the view.annotation reference that the delegate method very kindly gives you, you can use the map view's selectedAnnotations array and get the first object from that array (it will be the currently selected annotation). –  Sep 02 '14 at 14:19
  • my code crash here : – mounim Sep 02 '14 at 14:30
  • I recommend creating a custom annotation class (something that conforms to the MKAnnotation protocol) instead of using the plain MKPointAnnotation. This will let you add a "P_id" property to it and then you'll be able to easily retrieve it from the annotation object by casting it to your custom class. –  Sep 02 '14 at 14:30
  • @mounim, Please don't try to put code in comments. Update your question with more details and please specify _exactly_ what the crash message is (look in the Xcode console). Just saying "it crashes" is not helpful. –  Sep 02 '14 at 14:32
  • i used the Debugger .. it crashes at when it arrives to didReceiveMemoryWarning, so a memory problem ? – mounim Sep 02 '14 at 17:44

0 Answers0