在另一个屏幕上按下按钮后更改地图上图钉的颜色 [英] Change color of pin on map after pressing button on another screen
问题描述
我需要找到一种方法,在加载地图时通过按下另一个屏幕上的按钮来更改图钉颜色.
I need to find a way to change the pin color when the map is loaded by pushing a button from another screen.
例如,原来的图钉都是红色的,但是当我转到一个页面并按下地图按钮时,它必须将我带到地图视图,并且该位置的坐标必须用绿色图钉标记.
For example, the original pins are all red, but when I go to a page and push the map button, it has to lead me to the map view and the coordinates of that location have to be marked with a green pin.
我已经设置了地图以及将图钉设为红色的设置.
I already have the map set up and the setup of making the pins all red.
感谢任何帮助.
所以,简单回顾一下:带有红色图钉的页面 --> 单击图钉,单击注释(打开另一个视图)--> 内部视图,有一个按钮(例如@更改图钉颜色"),单击按钮 --> 带有绿色地图的页面销打开.(看上面带图片的例子.)
So, just a recap: Page with map with red pin --> click pin, click annotation (another view opens) --> inside view, there is a button (eg. @"change pin color"), click button --> page with map with green pin opens. (Look at the example above with pictures.)
推荐答案
为什么不直接声明一个新的构造函数来设置您选择的颜色?
Why don't you just declare a new constructor to set the colour of your choice?
// ViewControllerB .h file
@interface ViewControllerB
{
UIColor *pinColor;
}
-(id)initWithPinColor:(UIColor *)chosenPinColor;
...
// ViewControllerB .m file
-(id)initWithPinColor:(UIColor *)chosenPinColor
{
self = [super init];
if(self)
{
pinColor = chosenPinColor;
}
return self;
}
...
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MKUserLocation class]])
{
return nil;
}
static NSString *annotationViewID = @"annotationViewID";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewID];
if(!annotationView)
{
annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewID] autorelease];
}
// this will use the pinColor stored in the constructor
annotationView.pinColor = pinColor;
annotationView.canShowCallout = YES;
annotationView.animatesDrop = YES;
annotationView.annotation = annotation;
return annotationView;
}
然后你可以简单地在 ViewControllerA 中做到这一点:
Then you can simply do this in ViewControllerA:
// ViewControllerA .m file
#import "ViewControllerB.h"
...
-(void)showMapWithPinColor:(id)sender
{
ViewControllerB *vc = [[ViewControllerB alloc] initWithPinColor:[UIColor green]];
[self.navigationController pushViewController:vc animated:YES];
}
这篇关于在另一个屏幕上按下按钮后更改地图上图钉的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!