添加了额外代码
我是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
答案 0 :(得分:0)
如您所见,您的右键是UIButton
类型。因为这个事实你可以addTarget:
[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
}
答案 1 :(得分:0)
将右键按钮的标签设置为annID。
rightButton.tag = annId;
然后将Selector分配给touchup事件: [rightButton addTarget:self action:@selector(YourMethod :) forControlEvents:UIControlEventTouchUpInside];
在YourMethod中,您可以使用发件人标记导航到不同的视图
- (无效)YourMethod:(的UIButton *)发送方 { 如果(sender.tag == 1)
{
//推送viewcontroller1
}
否则
{
// push viewcontroller
}
返回; }
答案 2 :(得分:0)
检查:
- (无效)clickOnMapAnnotation:(的UIButton *)发送方
{
int AnnotationClicked = sender.tag;
if(AnnotationClicked ==1)
{
//PushViewcontroller1
}
else
{
//PushViewcontroller1
}
}
(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
if([注释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 = YES;
返回annView;
}