使用按钮自定义注释。 didSelectAnnotaionView似乎没有被调用

时间:2013-01-15 21:45:12

标签: iphone ios objective-c xcode ipad

我在使用自定义注释视图时调用didSelectAnnotationView时出现问题。我希望有人能够看一眼并帮助我解决我所缺少的问题。

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

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

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

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

// 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如下所示:

enter image description here

但是,我的didSelectAnnotationView永远不会被调用。任何人都可以解释我错过的东西吗?

请,任何帮助表示赞赏这一直让我疯狂。我可能完全错了,但是自定义视图确实出现了,所以我觉得我很接近。

感谢您的帮助!!

更新:

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

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

更新#2:

我的xib以这种方式加载:

在我的viewForAnnotation中,我调用了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;
}

1 个答案:

答案 0 :(得分:0)

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

你的didSelectAnnotationView在哪里?确保它实现了委托协议。 E.g。

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

然后在其他地方......

[mapView setDelegate:myView];