1

私はこの点を達成するのに苦労しています。NSObject のサブクラスであり、マップ上の注釈を表すクラスがあります。そのクラス内には、ユーザーが注釈を押したときにアラート ビューがあります。私がやろうとしているのは、ユーザーがアラートからOKを押した後、ピンの色を赤から緑に変更したいということです。

以下は私の 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

parentView (mapView) にピンを追加している行に警告が表示されます: Sending 'MKPinAnnotationView *__strong' to parameter of incompatible type 'id<MKAnnotation>' アプリを実行しているときに [OK] を押すとクラッシュします。何が間違っているのかわかりません。

前もって感謝します。

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 に答える