【发布时间】:2014-07-27 22:22:14
【问题描述】:
基本上我知道通常会出问题的地方(与我声明的方法不同),但看看这个。当然我可能会遗漏一些东西,但我已经尝试解决这个问题一个小时。我也打扫过了。
错误:DetailGymViewController.m:46:62:“CustomAnnotation”没有可见的@interface 声明选择器“initWithCoordinate:title:subtitle:”
错误代码:
#import "DetailGymViewController.h"
#import "CustomAnnotation.h"
@interface DetailGymViewController ()
@end
@implementation DetailGymViewController {
CLLocationManager *locationManager;
}
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
self.mapView.delegate = self;
self.nameLabel.text = self.name;
self.longitudeLabel.text = [NSString stringWithFormat:(@"%f"), self.longitude];
self.latitudeLabel.text = [NSString stringWithFormat:(@"%f"), self.latitude];
}
#pragma mark - Map/Locations delegates
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"Failed to get location with error: %@", error);
}
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
{
CLLocationCoordinate2D coordinate;
coordinate.latitude = self.latitude;
coordinate.longitude = self.longitude;
// ERROR HERE
CustomAnnotation *annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate title:self.name subtitle:@"Info"];
[self.mapView addAnnotation:(id)annotation];
}
CustomAnnotation.h 的声明:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface CustomAnnotation : NSObject
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *subtitle;
@property (nonatomic,assign) CLLocationCoordinate2D coordinate;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title subtitle:(NSString *)subtitle;
@end
还有,我的 CustomAnnotation.m 文件:
#import "CustomAnnotation.h"
@implementation CustomAnnotation
-(id) initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title subtitle:(NSString *)subtitle {
if ((self = [super init])) {
self.coordinate = coordinate;
self.title = title;
self.subtitle = subtitle;
}
return self;
}
@end
【问题讨论】:
-
您能否检查一下您是否错过了将 CustomAnnotation 添加到应用程序目标?
-
@ldindu 在哪里可以找到?奇怪的是我在另一个文件中使用了提到的 init 方法,我没有收到任何错误
-
你的
initWithCoordinate方法在哪里? -
好的,这意味着它们被添加到正确的目标上,那么可能还有其他问题。
-
@andrewbuilder 在我的 CustomAnnotation.h 文件中,它在我的帖子中。
标签: ios objective-c initialization declaration