我努力实现这一点。我有一个类,它是 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)上添加引脚的行上收到警告: Sending 'MKPinAnnotationView *__strong' to parameter of incompatible type 'id<MKAnnotation>'
当我运行应用程序时,按确定时崩溃。我不知道我做错了什么。
提前致谢。