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

これをチェックして:

-(void)clickOnMapAnnotation:(UIButton*)送信者

{

int AnnotationClicked =sender.tag;

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

}

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

    if ([注釈 isKindOfClass:[MKUserLocation クラス]]) return nil;

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

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

    //新しいView ControllerにつなげたいDetail Disclosureボタン。

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

    annView.rightCalloutAccessoryView = rightButton;

    annView.canShowCallout = はい;

    annView を返します。

}

于 2013-04-12T07:49:42.757 に答える
0

rightbutton のタグを annID に設定します。

rightButton.tag = annId;

次に、Selector を touchup イベントに割り当てます: [rightButton addTarget:self action:@selector(YourMethod:) forControlEvents:UIControlEventTouchUpInside];

YourMethod では、senders タグを使用して別のビューに移動できます

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

{

//viewcontroller1 をプッシュ

}

そうしないと

{

//ビューコントローラーをプッシュ

}

戻る; }

于 2013-04-11T08:16:27.037 に答える