我正在使用XCode 4.5.1构建一个iOS 6应用程序。我尝试使用协议使我的控制器可以检索图像。但是,尽管我这样调用它,但我的代理从未被调用:
UIImage *image = [self.delegate mapViewController:self imageForAnnotation:aView.annotation];
当我调试这一行时,我发现self.delegate
为空,尽管我已经将TableViewController关联为它的代理。
MapViewController.h:
@class MapViewController;
@protocol MapViewControllerDelegate <NSObject>
- (UIImage *)mapViewController:(MapViewController *)sender imageForAnnotation:(id <MKAnnotation>)annotation;
@end
@interface MapViewController : UIViewController
@property (nonatomic, weak) id <MapViewControllerDelegate> delegate;
@end
MapViewController.m:
@implementation MapViewController
@synthesize delegate = _delegate;
我正在使用TableViewController作为MapViewControllerDelegate:
@interface RecentPhotosTableViewController () <MapViewControllerDelegate>
- (PhotoViewController *) splitViewPhotoViewController;
@end
@implementation RecentPhotosTableViewController
- (UIImage *)mapViewController:(MapViewController *)sender imageForAnnotation:(id <MKAnnotation>)annotation
{
FlickrPhotoAnnotation *fpa = (FlickrPhotoAnnotation *)annotation;
NSURL *url = [FlickrFetcher urlForPhoto:fpa.photo format:FlickrPhotoFormatSquare];
NSData *data = [NSData dataWithContentsOfURL:url];
return data ? [UIImage imageWithData:data] : nil;
}
@end
@synthesize delegate = _delegate;
这行代码。编译器会自动处理。 - s1m0n