从MKMapKit中的注释标注按钮显示另一个视图控制器

时间:2012-08-08 05:32:22

标签: iphone objective-c uiviewcontroller mapkit mkannotation

我正在制作一个应用程序,它将使用MKMapKit作为引脚和注释。我想在一些文本之外显示更多信息,所以我在标注中创建了一个按钮,这样我就可以将它转到另一个视图控制器。目前,标注中的按钮不会执行任何操作,因为没有代码可以使其工作。如果有人可以让按钮的代码转到不同的视图控制器,那就太棒了!

头文件

#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

这是实施文件

#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;
}

我不确定这是否是我需要添加的内容,以及需要进入的内容

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

}

2 个答案:

答案 0 :(得分:1)

写下以下代码:

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

答案 1 :(得分:0)

首先你应该制作一个标签,你可以知道点击了哪个注释

moreInformationButton.tag = someIndex;

然后执行您想要的操作

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

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