如何使MKMapKit中注释中的详图索引按钮转到另一个视图控制器



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

这是头文件:

#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

使用entranceButton添加您在Touch Up Inside上注册的事件。

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

此外,在代码中,一旦entranceButton被分配给rightCalloutAccessoryView,就必须重新初始化它。

在地图中View:viewForAnnotation:delegate方法

-(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

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

最新更新