【问题标题】:'MyAnnotation' may not respond to 'initWithDictionary:'“MyAnnotation”可能不会响应“initWithDictionary:”
【发布时间】:2012-07-07 09:49:45
【问题描述】:

我不断遇到此代码的语义问题(“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

【问题讨论】:

    标签: iphone ios map annotations


    【解决方案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
    

    【讨论】:

    • 非常感谢,不敢相信我错过了,我已经看了好几个小时了,再次感谢
    猜你喜欢
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 2011-05-24
    • 2017-01-31
    相关资源
    最近更新 更多