Klipper
Klipper

Reputation: 5

Show another View Controller from an annotation callout button in MKMapKit

I am making an app that will use the MKMapKit for the pins and annotations. I want to display more information besides some text, so I created a button in the callout so I could make that go to another view controller. At the moment, the button in the callout doesn't do anything, as there is no code to make it work. It would be great if someone could make the code for the button to go to a different view controller!

The header file

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

#define METERS_PER_MILE 1609.344



@interface MSKSecondViewController :UIViewController<MKMapViewDelegate>
{
IBOutlet MKMapView *stillwellMapView;
int difficultyOfTrailOverlay;
}

@property (nonatomic, assign) CLLocationCoordinate2D mainEntranceToStillwellCoordinate;

@end

Here is the implementation file

#import "MSKSecondViewController.h"
@interface MSKSecondViewController ()

@end

@implementation MSKSecondViewController
@synthesize mainEntranceToStillwellCoordinate;


- (void)viewDidLoad
{

[super viewDidLoad];
stillwellMapView.delegate=self;
// Do any additional setup after loading the view, typically from a nib.
[self mainEntrancetoStillwellCoordinate];
[self bambooForestCoordinate];
}

- (void)mainEntrancetoStillwellCoordinate
{
MKPointAnnotation * main = [[MKPointAnnotation alloc]init];
CLLocationCoordinate2D mainLocation;
mainLocation.latitude = 40.831685;
mainLocation.longitude = -73.477453;
[main setCoordinate:mainLocation];
[main setTitle:@"Entrance"];
[main setSubtitle:@"Main"];
[stillwellMapView addAnnotation:main];
}

- (void)bambooForestCoordinate
{
MKPointAnnotation * bambooForest = [[MKPointAnnotation alloc]init];
CLLocationCoordinate2D bambooForestLocation;
bambooForestLocation.latitude = 40.829118;
bambooForestLocation.longitude = -73.466443;
[bambooForest setCoordinate:bambooForestLocation];
[bambooForest setTitle:@"Bamboo Forest"];
[bambooForest setSubtitle:@"Exit to Woodbury"];
[stillwellMapView addAnnotation:bambooForest];
}

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation 
(id<MKAnnotation>)annotation
{
MKPinAnnotationView * annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation      
reuseIdentifier:nil];
UIButton *moreInformationButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[moreInformationButton addTarget:self action:@selector(moreInformationButtonPressed:)     
forControlEvents:UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView = moreInformationButton;
moreInformationButton.frame = CGRectMake(0, 0, 23, 23);
moreInformationButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
moreInformationButton.contentHorizontalAlignment = 
UIControlContentHorizontalAlignmentCenter;


annView.pinColor =MKPinAnnotationColorGreen;
annView.animatesDrop = true;
annView.canShowCallout = TRUE;
return annView;
}

I'm not sure if this is what I need to add, and what needs to go inside here

-(void) moreInformationButtonPressed:(UIButton *)sender
{

}

Upvotes: 0

Views: 1154

Answers (2)

adali
adali

Reputation: 5977

first you should make a tag that you can know which annotation is clicked

moreInformationButton.tag = someIndex;

then do the action you want

-(void) moreInformationButtonPressed:(UIButton *)sender
{
    int index = sender.tag;

    YourController *yc =[[[YourController alloc] initWithIndex:index] autorelease];
    [self.navigationController pushViewController:scanView animated:YES];
}

Upvotes: 0

Apurv
Apurv

Reputation: 17186

Write below kind of code:

-(void) moreInformationButtonPressed:(UIButton *)sender
{
     OtherViewController *controller = [[OtherViewController alloc]initWithNibName:@"OtherViewController" bundle:nil];
     [self.navigationController pushViewController:controller animated:YES];
     [controller release];      
}

Upvotes: 1

Related Questions