Hi guys,
I have a mapview with a few pins on it. The pins are loading its data from a plist-file (title, longitude and latitude). I've also added a detailview where you get more info about the pin that gets tapped.
The pins are showing just fine on the map and I can tap on them to get to the detailview so my question is how do I send the, for example, the title from the mapview to my detailview when the disclosebutton is tapped?
I know it's something like this detailViewController.title = annotation.title, but I don't know how to fetch the annotation data.
Hope that somebody can get me on the right track
Here's my code:
RootViewController.h
RootViewController.m
MyAnnotation.h
MyAnnotation.m
I have a mapview with a few pins on it. The pins are loading its data from a plist-file (title, longitude and latitude). I've also added a detailview where you get more info about the pin that gets tapped.
The pins are showing just fine on the map and I can tap on them to get to the detailview so my question is how do I send the, for example, the title from the mapview to my detailview when the disclosebutton is tapped?
I know it's something like this detailViewController.title = annotation.title, but I don't know how to fetch the annotation data.
Hope that somebody can get me on the right track
Here's my code:
RootViewController.h
Code:
@interface RootViewController : UIViewController <MKMapViewDelegate> {
IBOutlet MKMapView *map;
NSArray *cam;
}
@end
RootViewController.m
Code:
- (void)viewDidLoad {
[super viewDidLoad];
map.delegate = self;
cam = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:@"Cam"
ofType:@"plist"]];
double minLat = [[cam valueForKeyPath:@"@min.latitude"] doubleValue];
double maxLat = [[cam valueForKeyPath:@"@max.latitude"] doubleValue];
double minLon = [[cam valueForKeyPath:@"@min.longitude"] doubleValue];
double maxLon = [[cam valueForKeyPath:@"@max.longitude"] doubleValue];
MKCoordinateRegion region;
region.center.latitude = (maxLat + minLat) / 2.0;
region.center.longitude = (maxLon + minLon) / 2.0;
region.span.latitudeDelta = (maxLat - minLat) * 1.05;
region.span.longitudeDelta = (maxLon - minLon) * 1.05;
map.region = region;
for (NSDictionary *camDict in cam){
MyAnnotation *annotation = [[MyAnnotation alloc] initWithDictionary:camDict];
[map addAnnotation:annotation];
[annotation release];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];
[annView setAnimatesDrop:YES];
[annView setCanShowCallout:YES];
[annView setSelected:YES];
[annView setUserInteractionEnabled: YES];
UIButton *discloseButton = [UIButton buttonWithType: UIButtonTypeDetailDisclosure];
[discloseButton addTarget: self action: @selector(showMyView:) forControlEvents: UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView = discloseButton;
return annView;
}
// Push to detailView with some data
- (IBAction)showMyView:(id)sender {
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
// something like this.
//detailViewController.title = annotation.title;
//detailViewController.tempAdress = annotation.subtitle;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
MyAnnotation.h
Code:
@interface MyAnnotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
MyAnnotation.m
Code:
- (id) initWithDictionary:(NSDictionary *) dict
{
self = [super init];
if (self != nil) {
coordinate.latitude = [[dict objectForKey:@"latitude"] doubleValue];
coordinate.longitude = [[dict objectForKey:@"longitude"] doubleValue];
self.title = [dict objectForKey:@"name"];
self.subtitle = [dict objectForKey:@"address"];
}
return self;
}