2012-10-15 62 views

回答

3

您正在嘗試設置名稱和說明註記的觀點的屬性,你應該使用titlesubtitle - 註釋對象 - MyAnnotationClass,註釋視圖將在呈現標註時使用此對象的標題和副標題。

我改變你的代碼在這裏工作:http://pastebin.com/YRGYhQev

@interface MyAnnotationClass : NSObject<MKAnnotation> 

@property (nonatomic, retain) NSString *title; 
@property (nonatomic, retain) NSString *subtitle; 
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 

-(id) initWithCoordinate:(CLLocationCoordinate2D) coordinate; 

@end 


MyAnnotationClass.m 

#import "MyAnnotationClass.h" 

@implementation MyAnnotationClass 

-(id) initWithCoordinate:(CLLocationCoordinate2D) coordinate{ 
    self=[super init]; 
    if(self){ 
     _coordinate = coordinate; 
    } 
    return self; 
} 

-(void) dealloc{ 
    [_title release]; 
    [_subtitle release]; 
    [super dealloc]; 
} 
@end 


ViewController.h 

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

@interface ViewController : UIViewController<MKMapViewDelegate> { 
    IBOutlet MKMapView *_myMapView; 
    NSArray *_myAnnotations; 
} 

@property (nonatomic, retain) NSArray *myAnnotations; 
@property (nonatomic, retain) IBOutlet MKMapView *myMapView; 

@end 


ViewController.m 

#import "ViewController.h" 
#import "AppDelegate.h" 
#import <MapKit/MapKit.h> 
#import <CoreLocation/CoreLocation.h> 
#import "PlaceMark.h" 
#import "MyAnnotationClass.h" 
@interface ViewController() 

@end 

@implementation ViewController 
@synthesize myMapView = _myMapView; 
@synthesize myAnnotations = _myAnnotations; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

-(void) viewDidLoad{ 
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate]; 

    //Initialize annotation 
    MyAnnotationClass *commuterLotAnnotation=[[MyAnnotationClass alloc] initWithCoordinate:CLLocationCoordinate2DMake(appDelegate.latitude , appDelegate.longitude)]; 
    commuterLotAnnotation.title = @"Hello title"; 
    commuterLotAnnotation.subtitle = @"Correct"; 

    MyAnnotationClass *overflowLotAnnotation=[[MyAnnotationClass alloc] initWithCoordinate:CLLocationCoordinate2DMake(appDelegate.latitude , appDelegate.longitude)]; 
    overflowLotAnnotation.title = @"Hello title"; 
    overflowLotAnnotation.subtitle = @"Correct"; 

    //Add them to array 
    self.myAnnotations=[NSArray arrayWithObjects:commuterLotAnnotation, overflowLotAnnotation, nil]; 

    //Release the annotations now that they've been added to the array 
    [commuterLotAnnotation release]; 
    [overflowLotAnnotation release]; 

    //add array of annotations to map 
    [_myMapView addAnnotations:_myAnnotations]; 
} 


-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation{ 
    static NSString *[email protected]"ParkingAnnotationIdentifier"; 

    if([annotation isKindOfClass:[MyAnnotationClass class]]){ 
     //Try to get an unused annotation, similar to uitableviewcells 
     MKAnnotationView *annotationView=[_myMapView dequeueReusableAnnotationViewWithIdentifier:parkingAnnotationIdentifier]; 

     //If one isn't available, create a new one 
     if(!annotationView){ 
      annotationView=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:parkingAnnotationIdentifier]; 
      //Here's where the magic happens 
      annotationView.image=[UIImage imageNamed:@"apple.gif"]; 
      annotationView.canShowCallout = YES; 
     } 
     return annotationView; 
    } 
    return nil; 
} 


- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 


@end 
1

我看到教程具有MyAnnotationClass接口爲:

@interface MyAnnotationClass : NSObject 

當我使用MKAnnotation餘設置的m個Ÿ標註接口起來就是:

@interface MyAnnotationClass : MKAnnotationView <MKAnnotation> 

然後在MyAnnotationClass.m文件我有以下幾種方法:

- (NSString *)title{ 
    return self.name; 
} 

- (NSString *)subtitle{ 
    return self.description; 
} 
+0

我試過了,但不工作 – Developer2012

+0

爲題方法曾經叫什麼名字? – Joe

+0

http://pastebin.com/03mDLc9q請參閱代碼 – Developer2012