0

我不断收到此代码的此语义问题('MyAnnotation' 可能不会响应'initWithDictionary:'),我使用 plist 向地图添加注释。

尽管它显示了 pin 和我想要的所有内容,但我遇到了语义问题并且似乎无法解决问题

如果有人可以提供帮助,那将非常感谢

继承人的代码,问题出在 //BrewMapViewController.m 错误在这一行

MyAnnotation *annotation = [[MyAnnotation alloc] initWithDictionary:breweryDict];

/*MyAnnotation.h*/
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MyAnnotation : NSObject <MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
    NSString *test;
}

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


@end


/*MyAnnotation.m*/
#import "MyAnnotation.h"
@implementation MyAnnotation
@synthesize coordinate, title, subtitle, test;

- (id) initWithDictionary:(NSDictionary *) dict
{
    self = [super init];
    if (self != nil) {
        coordinate.latitude = [[dict objectForKey:@"latitude"] doubleValue];
        coordinate.longitude = [[dict objectForKey:@"longitude"] doubleValue];
        self.title = [dict objectForKey:@"name"];
        self.subtitle = [dict objectForKey:@"address"];
        self.test = [dict objectForKey:@"test"];
    }
    return self;
}


@end


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

@interface BrewMapViewController : UIViewController <MKMapViewDelegate> {
    IBOutlet MKMapView *map;

    NSArray *breweries;
}

@end


/*BrewMapViewController.m*/

#import "BrewMapViewController.h"

#import "MyAnnotation.h"

@implementation BrewMapViewController


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    breweries = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] 
                                                         pathForResource:@"test" 
                                                         ofType:@"xml"]];

    double minLat = [[breweries valueForKeyPath:@"@min.latitude"] doubleValue];
    double maxLat = [[breweries valueForKeyPath:@"@max.latitude"] doubleValue];
    double minLon = [[breweries valueForKeyPath:@"@min.longitude"] doubleValue];
    double maxLon = [[breweries valueForKeyPath:@"@max.longitude"] doubleValue];

    MKCoordinateRegion region;
    region.center.latitude = (maxLat + minLat) / 2.0;
    region.center.longitude = (maxLon + minLon) / 2.0;
    region.span.latitudeDelta = (maxLat - minLat) * 1.05;
    region.span.longitudeDelta = (maxLon - minLon) * 1.05;
    map.region = region;

    for (NSDictionary *breweryDict in breweries){
        MyAnnotation *annotation = [[MyAnnotation alloc] initWithDictionary:breweryDict];
        [map addAnnotation:annotation];
        [annotation release];
    }
}


- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{

    if (map.userLocation == annotation){
        return nil;
    }

    NSString *identifier = @"MY_IDENTIFIER";

    MKAnnotationView *annotationView = [map dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (annotationView == nil){
        annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation 
                                                       reuseIdentifier:identifier] 
                          autorelease];
        annotationView.image = [UIImage imageNamed:@"beer.png"];
        annotationView.canShowCallout = YES;

        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

        annotationView.leftCalloutAccessoryView =  [[[UIImageView  alloc] initWithImage:[UIImage imageNamed:@"pretzel.png"]] autorelease];

    }
    return annotationView;
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"I've been tapped");
}

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


- (void)dealloc {
    [breweries release];
    [map release];
    [super dealloc];
}

@end
4

1 回答 1

1

您必须将方法签名- (id) initWithDictionary:(NSDictionary *) dict放入您的头文件中,以告知BrewMapViewController该方法存在:

/*MyAnnotation.h*/
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MyAnnotation : NSObject <MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
    NSString *test;
}

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

- (id) initWithDictionary:(NSDictionary *) dict;

@end
于 2012-07-06T23:27:13.023 回答