2013-01-15 79 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

相關問題