作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不断遇到此代码的语义问题(“MyAnnotation”可能不会响应“initWithDictionary:”),我使用 plist 将注释添加到 map 。
尽管它显示了 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
最佳答案
您必须将 - (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
关于iphone - “MyAnnotation”可能无法响应 'initWithDictionary:',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11370623/
我不断遇到此代码的语义问题(“MyAnnotation”可能不会响应“initWithDictionary:”),我使用 plist 将注释添加到 map 。 尽管它显示了 pin 和我想要的一切,但
我是一名优秀的程序员,十分优秀!