0

I have a json file with some information I want to show this information on map, I can show Long and Lat on map, when you click on Annotation I have a button I want to show detail of my json on detail view, my problem is that I donot know how to show/send information on detail view based on annotation clicked,

Here is how I will load my detailViwe

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
calloutAccessoryControlTapped:(UIControl *)control
{
    DetailViewController *detail = [[DetailViewController alloc] initWithNibName:nil 
bundle:nil];

    // my question is here In my detail view I have label like status, company, how to add json info here

    detail.status.text = ??!!!
    detail.company.text = ??!!!

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

In my Log I have the correct status printed but in my detail view controller I have null printed

- (void)viewDidLoad
{
[super viewDidLoad];

self.status.text = _status.text;
    NSLog(@"Status %@ ",_status );
    NSLog(@"Status is %@ ",self.status.text);

}

Print is Status null // // // Status is null

Sara Shumika
  • 81
  • 1
  • 8

1 Answers1

1

You can access data from the MKAnnotationView provided in

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

The view object has an annotation property that will give you an object adopting the MKAnnotation protocol. This could be MKPointAnnotation as you already have, if just a title and subtitle will do. But you could also define a custom annotation class that holds onto a status and a company:

MyAnnotation *annotation = view.annotation; 
// annotation.status
// annotation.company

You'd have to create a MyAnnotation instance and insert the data in the place where you currently are creating newAnnotation.

As for once you have the data you need and you want to pass it to the DetailViewController, I suggest looking at this SO answer or Ole Begemann's tips here. In short, you can create a public property of your detail view controller, and then do something like:

    DetailViewController *destinationController = [[DestinationViewController alloc] init];
    destinationController.name = annotation.status;
    [self.navigationController pushViewController:destinationController animated:YES];

In summary, your method could then look something like this:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
calloutAccessoryControlTapped:(UIControl *)control
{
    MyAnnotation *annotation = view.annotation; 

    DetailViewController *detail = [[DetailViewController alloc] initWithNibName:nil 
bundle:nil];

    detail.status = annotation.status;
    detail.company = annotation.company;

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

And then set the UILabel texts in your detail view controller:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.statusTextField.text = self.status;
    self.companyTextField.text = self.company;
}

Update to clarify the creation of MyAnnotation:

You always have the option of creating a custom class. Here could be an example for MyAnnotation.h:

#import <MapKit/MapKit.h>

@interface MyAnnotation : MKPointAnnotation

@property (strong, nonatomic) NSString *status;
@property (strong, nonatomic) NSString *company;

@end

Then in your map view controller import: #import "MyAnnotation.h"

and use MyAnnotation instead of MKPointAnnotation:

// create the annotation
newAnnotation = [[MyAnnotation alloc] init];
newAnnotation.title = dictionary[@"applicant"];
newAnnotation.subtitle = dictionary[@"company"];
newAnnotation.status = dictionary[@"status"];
newAnnotation.company = dictionary[@"company"];
newAnnotation.coordinate = location;  
[newAnnotations addObject:newAnnotation];
Community
  • 1
  • 1
matt---
  • 2,470
  • 2
  • 20
  • 20
  • thanks for reply, would you please write the complete answer, I mean for this custom part MyAnnotation *annotation = view.annotation; annotation.status ... based on my code, Thanks in advance – Sara Shumika Jul 21 '14 at 23:16
  • thanks again I have one more question, I add a label on my detail view and I try it with status but my label is null, do you know what is wrong here? I added edit part to my question – Sara Shumika Jul 22 '14 at 00:30
  • I'm not positive but I think your null label issue should be in a new SO question, otherwise this question starts to creep away from the original issue. – matt--- Jul 22 '14 at 18:05