2013-01-15 43 views
1

我在使用自定义注释视图时遇到了我的didSelectAnnotationView被调用的问题。我希望有人能够看看我的缺点并帮助我。使用按钮进行自定义注释。 didSelectAnnotaionView似乎并没有被调用

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

我增加了自定义的注释是这样的:上面使用

LocationObject * obj = [m_MyObject.marLocations objectAtIndex:page]; 
obj.title [email protected]"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 

而对于上述观点的厦门国际银行是这样的:

enter image description here

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

请,任何帮助表示赞赏,这一直驱使我坚果。我可能完全错误,但自定义视图确实出现,所以我认为我很接近。

感谢您的帮助!

更新:

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

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

更新#2:

我的厦门国际银行加载这样:

在我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; 
} 
+0

如何被精确地加载厦门国际银行和你设置注释视图的框架?关于viewForAnnotation中代码的无关说明:其中大部分内容效率低下且不必要的复杂。 – Anna

+0

我对注解视图框架没做任何事情。你能解释什么是复杂的吗?如果你指的是我的代码,我愿意接受任何建议。我已更新我的帖子以显示如何加载xib。 – LilMoke

+0

尝试将注解视图的框架设置为xib的大小。 xib可能大于默认框架,并且不会处理外部触摸。在viewForAnnotation中,不必要的复杂性是通过计算页面等来“查找”注释。注释已经作为方法的参数给出 - 您不需要找到它或设置它的坐标。 – Anna

回答

0

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

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

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

然后别的地方...

[mapView setDelegate:myView]; 
+0

创建地图视图的视图是一个ViewController并设置为委托,并且在该视图的头文件中有。我已经检查了一百次,我认为这一定是别的。还有其他建议吗? – LilMoke

相关问题