2013-02-23 64 views
0

我是子類的一個實現MKMapViewDelegate的類。我也在超類中設置委託,但是我的-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation方法沒有被調用。這裏是我的代碼子類化一個實現MKMapViewDelegate的類,但是MKMapViewDelegate方法不被調用

我超碼

// RecentPhotosMapViewController.h 
@interface RecentPhotosMapViewController : UIViewController <MKMapViewDelegate> 

    @property (nonatomic,strong) NSArray *annotations; 
    @property(nonatomic,weak) IBOutlet MKMapView *mapView; 

@end 


// RecentPhotosMapViewController.m 
- (void)viewDidLoad{ 

     [super viewDidLoad]; 
// Do any additional setup after loading the view. 

    // load annotation data 



     self.mapView.delegate = self; 
} 

    - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation: (id<MKAnnotation>)annotation{ 

MKAnnotationView *aView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"MapVC"]; 

    // safety code 
    if(!aView){ 
     aView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MapVC"]; 
     aView.canShowCallout = YES; 
     aView.leftCalloutAccessoryView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)]; 
     aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

     } 
    aView.annotation = annotation; 
    [(UIImageView *)aView.leftCalloutAccessoryView setImage:nil]; 

    return aView; 
} 

我的子類代碼

 // RecentPhotosMapViewControllerWithAnnotationData.h 
    @interface RecentPhotosMapViewControllerWithAnnotationData : RecentPhotosMapViewController 
    @end 

RecentPhotosMapViewControllerWithAnnotationData.m文件

 -(void) viewDidLoad{ 


// extract annotation data..... 
// set zoom level 


[super viewDidLoad]; 

} 

然而-(void) mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view method is called

任何幫助表示讚賞

回答

1

嘗試移動[super viewDidLoad];作爲子類中的第一行viewDidLoad

通過這臺委託調用[super viewDidLoad]的時候,一切都已經發生了。

+0

感謝它幫助,但首先我需要做的設置我的模式之前我[超級viewDidLoad中]被稱爲在我的子類的任何其他建議。 – shaunak1111 2013-02-23 06:03:31

+0

你可能要重新考慮你爲什麼這樣做,但你可以前的'[超級viewDidLoad中]還可以將一些東西;'但要記住,有可能是做你想要什麼更好的辦法。 – Ric 2013-02-23 06:09:20

0

保持的MapView委託指派行派生類viewDidLoad方法的最後一行:

RecentPhotosMapViewControllerWithAnnotationData.m

-(void) viewDidLoad 
{ 

// extract annotation data..... 
// set zoom level 

[super viewDidLoad]; 

self.mapView.delegate = self; 

} 
相關問題