如何在MKMapKit中的注释中创建callout按钮转到另一个视图控制器

时间:2012-08-07 00:26:21

标签: iphone objective-c mkmapview mapkit

我希望标注中的按钮转到另一个视图控制器以显示额外信息,然后能够返回到地图视图。地图位于选项卡式视图控制器内。目前,如果您单击标注中的按钮,应用程序崩溃并给您一个线程错误。目前还不确定该做什么。

这是头文件:

#import UIKit/UIKit.h>
#import MapKit/MapKit.h>
#define METERS_PER_MILE 1609.344

@interface MSKSecondViewController :UIViewController<MKMapViewDelegate>
{
    IBOutlet MKMapView *stillwellMapView;
}
@property (nonatomic, assign) CLLocationCoordinate2D mainEntranceToStillwellCoordinate;
@end

这是实现文件:

@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];
}

- (void)viewDidUnload
{
    stillwellMapView = nil;
    [super viewDidUnload]; 
    // Release any retained subviews of the main view.
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation: 
(id<MKAnnotation>)annotation    
{
    MKPinAnnotationView * annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
    UIButton *entranceButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [entranceButton addTarget:self action:@selector(entranceButtonPressed:) 
forControlEvents:UIControlEventTouchUpInside];
    annView.rightCalloutAccessoryView = entranceButton;
    entranceButton = [UIButton buttonWithType:UIButtonTypeCustom];
    entranceButton.frame = CGRectMake(0, 0, 23, 23);
    entranceButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    entranceButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [UIButton buttonWithType:UIButtonTypeDetailDisclosure];  
    annView.animatesDrop=TRUE;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);
    return annView;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)viewWillAppear:(BOOL)animated 
{  
    //the coordinates in which the map shows once loaded
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = 40.831922;
    zoomLocation.longitude= -73.476353; 
    //the amount of area shown by the map when it loads
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation,  
0.15*METERS_PER_MILE, 0.15*METERS_PER_MILE); 
    MKCoordinateRegion adjustedRegion = [stillwellMapView regionThatFits:viewRegion];                
    [stillwellMapView setRegion:adjustedRegion animated:YES]; 
}
@end

2 个答案:

答案 0 :(得分:1)

使用entranceButton添加您在Touch Up Inside上注册的活动。

-(void) entranceButtonPressed:(UIButton *)sender
{
//Write the controller push code here
}

此外,在代码中,一旦将其分配给rightCalloutAccessoryView,您就重新初始化了入口按钮。

答案 1 :(得分:0)

在mapView:viewForAnnotation:委托方法

-(MKAnnotationView *)mapView:(MKMapView *)mapView
        viewForAnnotation:(id <MKAnnotation>)annotation 

//添加以下行

UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.rightCalloutAccessoryView = rightButton;

然后实现代码以导航到以下委托方法中的其他视图控制器

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

确保视图控制器嵌入在导航控制器中