2010-10-21 57 views
1

我無法在MapKit中更改註釋的pinColor。如果我不嘗試實施的MapView:viewForAnnotation:方法一切正常(註釋添加),但是當我試圖改變註解視圖,模擬器崩潰:改變Pin的顏色iOS MapKit

下面是代碼:

MapViewController.h

#import "MapViewController.h" 
#import <MapKit/MapKit.h> 
#import <CoreLocation/CoreLocation.h> 
#import "Annotation.h" 


@implementation MapViewController 

@synthesize myMapView; 

/* 
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { 
     // Custom initialization 
    } 
    return self; 
} 
*/ 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    CLLocationCoordinate2D location; 
    location.longitude = 2.21; 
    location.latitude = 48.5; 
    MKCoordinateSpan span; 
    span.latitudeDelta = 1*(1 - 0); 
    span.longitudeDelta = 1*(1 - 0); 
    MKCoordinateRegion region; 
    region.span = span; 
    region.center = location; 
    [myMapView setRegion:region animated:NO]; 
    [myMapView regionThatFits:region]; 
    Annotation *someAnnotation =[[Annotation alloc] init]; 
    [myMapView addAnnotation:someAnnotation]; 
    } 



/* 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 


- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id<MKAnnotation>) annotation { 
    MKPinAnnotationView *customPinview = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil] autorelease]; 
    customPinview.pinColor = MKPinAnnotationColorGreen; 
    customPinview.animatesDrop = YES; 
    customPinview.canShowCallout = YES; 
    return customPinview; 
} 



- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 
} 


@end 

MapViewController.m

// 
// MapViewController.h 
// TestMap 
// 
// Created by Johan Ismael on 10/21/10. 
// Copyright 2010 __MyCompanyName__. All rights reserved. 
// 

#import <UIKit/UIKit.h> 
#import <MapKit/MapKit.h> 
#import <CoreLocation/CoreLocation.h> 


@interface MapViewController : UIViewController<MKMapViewDelegate> { 

@private 

    IBOutlet MKMapView *myMapView; 

} 

@property (nonatomic, retain) IBOutlet MKMapView *myMapView; 
//- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id<MKAnnotation>) annotation; 

@end 

在此先感謝!

注:MapViewController中被聲明爲的MapView的IB

回答

3

在viewForAnnotation方法的委託,該ALLOC正在對MKAnnotationView而不是MKPinAnnotationView完成。它必須與「無法識別的選擇器」崩潰,因爲MKAnnotationView沒有pinColor屬性。更改爲:

MKPinAnnotationView *customPinview = [[[MKPinAnnotationView alloc] 
    initWithAnnotation:annotation reuseIdentifier:nil] autorelease]; 
+0

謝謝!併爲這個非常愚蠢的問題抱歉... :( – Johanisma 2010-10-22 05:05:41

+0

我用你的方法,但我的標題和副標題不能顯示... – 2012-04-11 10:08:58