1

我努力实现这一点。我有一个类,它是 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>' 当我运行应用程序时,按确定时崩溃。我不知道我做错了什么。

提前致谢。

4

1 回答 1

5

我正在试一试,不确定它是否会起作用。

在注释中添加一个属性:

@property (nonatomic) BOOL annotationWasSelected;

这将设置为TRUE当用户关闭 UIAlertView 时(将其初始化为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;
}

您必须将此代码与现有代码合并。

同样,我不完全确定此代码,现在无法对其进行测试。让我知道它是否有帮助。

于 2012-11-22T15:05:02.677 回答