0

添加了额外的代码

我是 xcode 的新手。我设法在我的地图上创建了各种不同颜色的注释。我想做的是让每个注释都指向带有披露按钮的新位置。我已经实现了一个右箭头显示按钮,现在我需要知道的是如何将它链接起来,以便每个注释都会导致我选择的不同视图控制器。这是我的 MapViewController 实现的当前代码。

MainMapViewController.m

#import "MainMapViewController.h"
#import "LocationAnnotation.h"

@interface MainMapViewController ()


@end

//Totnes Main Centre Coordinates
#define Totnes_LATITUDE 50.433741
#define Totnes_LONGITUDE -3.685797

//The Dartmouth Inn Coordinates
#define DARTMOUTH_INN_LATITUDE 50.430036;
#define DARTMOUTH_INN_LONGITUDE -3.683873;

//Pub Offers Co-Ordinates

#define TheKingBill_LATITUDE 50.431379
#define TheKingBill_LONGITUDE -3.685495

#define TheSevenStars_LATITUDE 50.431045
#define TheSevenStars_LONGITUDE -3.682945

#define TheLordNelson_LATITUDE 50.430931
#define TheLordNelson_LONGITUDE -3.683644

//Span
#define THE_SPAN 0.01f;


@implementation MainMapViewController

@synthesize mainMapView;

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Set Delegate
    mainMapView.delegate = self;

    //Create the region
    MKCoordinateRegion myRegion;

    //Centre
    CLLocationCoordinate2D centre;
    centre.latitude = Totnes_LATITUDE;
    centre.longitude = Totnes_LONGITUDE;

    //Span
    MKCoordinateSpan span;
    span.latitudeDelta = THE_SPAN;
    span.longitudeDelta = THE_SPAN;

    myRegion.center = centre;
    myRegion.span = span;


    //Set The Map View
    [mainMapView setRegion:myRegion animated:YES];


    //Annotation

    NSMutableArray * locations = [[NSMutableArray alloc] init];
    LocationAnnotation * myAnn;

    //The King Bill Annotation
    myAnn = [[LocationAnnotation alloc] initWithTitle:@"The King Bill"
                                          andSubtitle:@"Another Pub In Town"
                                        andCoordinate:CLLocationCoordinate2DMake(TheKingBill_LATITUDE, TheKingBill_LONGITUDE)
                                                andID:1];
    [locations addObject:myAnn];

    //The Seven Stars Annotations
    myAnn = [[LocationAnnotation alloc] initWithTitle:@"The Royal Seven Stars Hotel"
                                          andSubtitle:@"Hotel In Town"
                                        andCoordinate:CLLocationCoordinate2DMake(TheSevenStars_LATITUDE, TheSevenStars_LONGITUDE)
                                                andID:2];
    [locations addObject:myAnn];

    //The Lord Nelson Annotations
    myAnn = [[LocationAnnotation alloc] initWithTitle:@"The Lord Nelson"
                                          andSubtitle:@"Great Pub In Centre of Town"
                                        andCoordinate:CLLocationCoordinate2DMake(TheLordNelson_LATITUDE, TheLordNelson_LONGITUDE)
                                                andID:3];
    [locations addObject:myAnn];

    [self.mainMapView addAnnotations:locations];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - MKMapViewDelegate

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation {
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];

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


    int annId = ((LocationAnnotation *)annotation).idNumber;
    annView.pinColor = (annId == 1) ? MKPinAnnotationColorPurple
    : (annId == 2) ? MKPinAnnotationColorGreen
    : MKPinAnnotationColorRed;
    annView.canShowCallout = YES;
    return annView;


}


@end
4

3 回答 3

0

如您所见,您的右键类型为UIButton。由于这个事实,您可以向其添加目标:

[rightButton addTarget:self action:@selector(YourMethod) forControlEvents:UIControlEventTouchUpInside];

用户点击rightButton YourMethod后将启动。因此,对于每个引脚,您可以使用不同的方法添加不同的按钮,以您设置的相同方式AnnotationColor

int annId = ((LocationAnnotation *)annotation).idNumber;
if (annId == 1)
{
    annView.pinColor = MKPinAnnotationColorPurple;
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    annView.rightCalloutAccessoryView = rightButton;
    [rightButton addTarget:self action:@selector(MethodWhereYouWillFireYouNewViewController) forControlEvents:UIControlEventTouchUpInside];

}
else if (annId == 2)
{
    //similar as above
}
于 2013-04-11T08:10:33.590 回答
0

检查这个:

-(void)clickOnMapAnnotation:(UIButton*)sender

{

int AnnotationClicked =sender.tag;

if(AnnotationClicked ==1)
{
    //PushViewcontroller1
}
else
{
      //PushViewcontroller1
}

}

  • (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id) 注释 {

    if ([annotation isKindOfClass:[MKUserLocation class]]) 返回零;

    MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];

    int annId = ((LocationAnnotation *)annotation).idNumber; annView.pinColor = (annId == 1) ?MKPinAnnotationColorPurple : (annId == 2) ?MKPinAnnotationColorGreen : MKPinAnnotationColorRed;

    //我要引导到新视图控制器的详细信息披露按钮。

    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; [rightButton addTarget:self action:@selector(clickOnMapAnnotation:) forControlEvents:UIControlEventTouchUpInside]; rightButton.tag = AnnId;

    annView.rightCalloutAccessoryView = rightButton;

    annView.canShowCallout = 是;

    返回安视图;

}

于 2013-04-12T07:49:42.757 回答
0

将 rightbutton 的标签设置为 anniID。

rightButton.tag = AnnId;

然后将 Selector 分配给 touchup 事件: [rightButton addTarget:self action:@selector(YourMethod:) forControlEvents:UIControlEventTouchUpInside];

在 YourMethod 中,您可以使用 senders 标签导航到不同的视图

-(void)YourMethod:(UIButton*)sender { if(sender.tag==1)

{

//推送视图控制器1

}

别的

{

//推送视图控制器

}

返回; }

于 2013-04-11T08:16:27.037 回答