1

当我使用自定义注释视图时,我的 didSelectAnnotationView 被调用时遇到问题。我希望有人能够看看并帮助我解决我所缺少的东西。

我实际上希望在用户触摸我的自定义注释屏幕截图中显示的按钮时调用一个方法,如下所示。我试图制作 calloutAccessoryView,但我做错了什么。如果有人完成了用户可以点击的自定义注释视图,请查看您是否可以提供帮助!这真让我抓狂。我想我已经在这里发布了相关信息,如果不问,我会编辑我的帖子。

我正在添加这样的自定义注释:

LocationObject * obj = [m_MyObject.marLocations objectAtIndex:page];
obj.title =@"A";
[mvMapView addAnnotation:obj];

上面使用的我的 Location 对象定义如下:

// Location Object
@interface LocationObject : NSObject <MKAnnotation>

@property (nonatomic, retain) NSString * practicename;
@property (nonatomic, retain) NSString * address1;
@property (nonatomic, retain) NSString * mapaddress1;
@property (nonatomic, retain) NSString * address2;
@property (nonatomic, retain) NSString * city;
@property (nonatomic, retain) NSString * state;
@property (nonatomic, retain) NSString * zip;
@property (nonatomic, retain) NSString * phone;
@property (nonatomic, retain) NSString * fax;
@property (nonatomic, retain) NSString * email;
@property (nonatomic, retain) NSString * longitude;
@property (nonatomic, retain) NSString * latitude;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

@end // End Location Object

我的 viewForAnnotation 这样做:

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
        [map.userLocation setTitle:@"I am here"];
        return nil;
    }

    static NSString* AnnotationIdentifier = @"annotationViewID";
    ViewDirectionsAnnotationView * annotationView = [[ViewDirectionsAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
    int page = floor((svOfficesScrollView.contentOffset.x - svOfficesScrollView.frame.size.width / 2) / svOfficesScrollView.frame.size.width) + 1;
    LocationObject * obj = [m_DentistObject.marLocations objectAtIndex:page];
    double dLatitude = [obj.latitude doubleValue];
    double dLongitude = [obj.longitude doubleValue];
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(dLatitude, dLongitude);
    ((LocationObject*)annotation).coordinate = coordinate;
    annotationView.lbPracticeName.text = obj.practicename;
    annotationView.lbStreet.text = obj.address1;
    NSString * sCityStateZip = [NSString stringWithFormat:@"%@, %@ %@", obj.city, obj.state, obj.zip];
    annotationView.lbCityStateZip.text = sCityStateZip;
    annotationView.lbPhoneNumber.text = obj.phone;
    annotationView.canShowCallout = YES;
    annotationView.annotation = annotation;
    annotationView.rightCalloutAccessoryView = (UIButton *)[annotationView viewWithTag:1]; //[UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    return annotationView;
}

我的注释视图是这样的:

@interface ViewDirectionsAnnotationView : MKAnnotationView
{
    CLLocationCoordinate2D    coordinate;
    NSString        * title;
    NSString        * subtitle;
}

@property (nonatomic, retain) IBOutlet UIView * loadedView;
@property (weak, nonatomic) IBOutlet UILabel  * lbPracticeName;
@property (weak, nonatomic) IBOutlet UILabel  * lbStreet;
@property (weak, nonatomic) IBOutlet UILabel  * lbCityStateZip;
@property (weak, nonatomic) IBOutlet UILabel  * lbPhoneNumber;

@end

上述视图的 xib 如下所示:

在此处输入图像描述

但是,我的 didSelectAnnotationView 永远不会被调用。谁能解释我错过了什么?

请,感谢任何帮助,这让我发疯了。我可能完全错了,但自定义视图确实出现了,所以我认为我很接近。

谢谢您的帮助!!

更新:

我忘了提到代表。我已经多次检查了代表,并相信我是对的。在 mapView 所在的 ViewController .h 文件中,我设置了 [mapView setDelegate:self] 在同一个 .m 文件中,我实现了 viewForAnnotation 方法和 didSelectAnnotationView。viewForAnnotation 被调用,但不是另一个。

一条更有趣的信息是,如果我在自定义注释下方单击(触摸),则会调用 didSelectAnnotationView。我想我现在更困惑了!!

更新#2:

我的 xib 是这样加载的:

在我看来ForAnnotation 我称之为initWithAnnotation。该方法如下:

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString*)reuseIdentifier
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    if (self != nil)
    {
        [[NSBundle mainBundle] loadNibNamed:@"DirectionsAnnotation" owner:self options:nil];
        if (loadedView)
        {
            [loadedView setFrame:CGRectMake(-(loadedView.frame.size.width/2), -(loadedView.frame.size.height), loadedView.frame.size.width, loadedView.frame.size.height)];
            [self addSubview:loadedView];
        }
    }
    return self;
}
4

1 回答 1

0

显示您的 MapView 的视图需要设置为您的委托。

你的 didSelectAnnotationView 在哪里?确保它实现了委托协议。例如

@interface MyView : UIViewController <MKMapViewDelegate>
...
@end

然后在别的地方...

[mapView setDelegate:myView];
于 2013-01-15T22:30:22.807 回答