我努力实现这一点。我有一个类是NSObject的子类,并在地图上表示一个注释。在该类中,当用户按下注释时,我有一个警报视图。我想要做的是,在用户按下警告后,我想将引脚的颜色从红色更改为绿色。
以下是我的MKAnnotation课程......
·H
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import "MapMenu.h"
@interface BuildingViewController : NSObject <MKAnnotation, MapMenuDelegate>{
NSString *name;
NSString *description;
CLLocationCoordinate2D location;
NSString *owner; /*user object*/
}
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *owner;
@property (nonatomic, retain) IBOutlet MapMenu *menu;
- (id) initBuildingWithName: (NSString *) _name andCoordinates: (CLLocationCoordinate2D) _location
shortDescription:(NSString *)_description andOwner:(NSString *)_owner inDistance: (NSInteger) _distance;
- (void) addMenuOnView: (MKMapView*) mapView;
- (void) hideMenuFromView;
- (void) setAttackEnable: (BOOL) attack;
- (void) attackTo: (BuildingViewController*) selectedBuilding;
@end
的.m
#import "BuildingViewController.h"
#import "CountDownTimer.h"
@implementation BuildingViewController{
MapMenuItem* starMenuItem1, *starMenuItem2,
*starMenuItem3, *starMenuItem4;
NSInteger distance;
MKMapView* parentView;
}
@synthesize title, subtitle, coordinate, owner, menu = _menu;
- (id) initBuildingWithName:(NSString *)_name andCoordinates:(CLLocationCoordinate2D)_location
shortDescription:(NSString *)_description andOwner:(NSString *)_owner inDistance:(NSInteger)_distance{
self = [super init];
title = _name;
coordinate = _location;
subtitle = _description;
owner = _owner;
distance = (int)round(_distance);
UIImage *storyMenuItemImage = [UIImage imageNamed:@"bg-menuitem.png"];
UIImage *storyMenuItemImagePressed = [UIImage imageNamed:@"bg-menuitem-highlighted.png"];
UIImage *starImage = [UIImage imageNamed:@"icon-star.png"];
starMenuItem1 = [[MapMenuItem alloc] initWithImage:storyMenuItemImage highlightedImage:storyMenuItemImagePressed
contentImage:starImage highContentImage:nil isDisable:NO];
starMenuItem2 = [[MapMenuItem alloc] initWithImage:storyMenuItemImage highlightedImage:storyMenuItemImagePressed
contentImage:starImage highContentImage:nil isDisable:NO];
starMenuItem3 = [[MapMenuItem alloc] initWithImage:storyMenuItemImage highlightedImage:storyMenuItemImagePressed
contentImage:starImage highContentImage:nil isDisable:NO];
starMenuItem4 = [[MapMenuItem alloc] initWithImage:storyMenuItemImage highlightedImage:storyMenuItemImagePressed
contentImage:starImage highContentImage:nil isDisable:NO];
if (distance > 10){
[self setAttackEnable:NO];
starMenuItem1.disable = YES;
}
NSArray *menus = [NSArray arrayWithObjects:starMenuItem1, starMenuItem2, starMenuItem3, starMenuItem4, nil];
_menu = [[MapMenu alloc] initWithFrame:self.menu.bounds menus:menus];
_menu.delegate = self;
return self;
}
- (void) addMenuOnView:(MKMapView *)mapView{
parentView = mapView;
[parentView addSubview: _menu];
}
- (void) hideMenuFromView{
[_menu removeFromSuperview];
}
- (void) setAttackEnable:(BOOL)attack{
if (attack) {
[starMenuItem1 setHighlighted:NO];
}else{
[starMenuItem1 setHighlighted:YES];
[starMenuItem1 setImage:[UIImage imageNamed:@"bg-menuitem-highlighted.png"]];
}
}
- (void)MapMenu:(MapMenu *)menu didSelectButton:(NSInteger)index{
NSLog(@"Select the index : %d\n",index);
NSLog(@"%@", self.owner);
if (index == 0) {
if (self.owner == nil && distance < 10) {
CountDownTimer* countDown = [[CountDownTimer alloc]init];
[countDown startTimerOn:parentView];
[self performSelector:@selector(attackTo:) withObject:nil afterDelay:20.0];
}
else if (self.owner == @"Player_1")
NSLog(@"You have already occupy this building with name, %@", self.title);
}
}
- (void) attackTo: (BuildingViewController*) selectedBuilding{
self.owner = @"Player_1";
[self showMessage:@"Success" withContent:@"You are the new owner of the building."];
//NSLog(@"Building has a new owner with name, %@", self.owner);
}
- (void) showMessage: (NSString*) msgTitle withContent: (NSString*) content{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:msgTitle
message:content
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
//I want to change the pin colour here.
}
}
@end
我在父视图(mapView)上添加pin的行上收到警告:Sending 'MKPinAnnotationView *__strong' to parameter of incompatible type 'id<MKAnnotation>'
当我运行应用程序时,按OK时会崩溃。我不知道我做错了什么。
提前致谢。
答案 0 :(得分:5)
我正在试一试,不确定它会起作用。
在注释中添加属性:
@property (nonatomic) BOOL annotationWasSelected;
当用户解除UIAlertView(将其初始化为TRUE
)时,这将设置为FALSE
。
所以用以下代码替换你的代码:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
_annotationWasSelected = TRUE;
// Remove and add the annotation so it's re-drawn
[parentView removeAnnotation:self];
[parentView addAnnotation:self];
}
}
在mapView的委托中,你应该做这样的事情:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = @"MyAnnotation";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
YourAnnotationClass *myAnnotation = (YourAnnotationClass*) annotation;
// If a new annotation is created
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:identifier];
// Annotation's color
if (myAnnotation.annotationWasSelected) {
annotationView.pinColor = MKPinAnnotationColorGreen;
}
else {
annotationView.pinColor = MKPinAnnotationColorRed;
}
} else {
annotationView.annotation = annotation;
// Annotation's color
if (myAnnotation.annotationWasSelected) {
annotationView.pinColor = MKPinAnnotationColorGreen;
}
else {
annotationView.pinColor = MKPinAnnotationColorRed;
}
}
return annotationView;
}
您必须将此代码与现有代码合并。
同样,我不完全确定这段代码,现在无法测试。如果有帮助,请告诉我。