As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center提供指导。




7年前关闭。




我正在使用XCode 4.5.1构建iOS 6应用

我正在尝试使用控制器可以用来检索图像的协议。但是,尽管我这样称呼我,但从未调用过我的代表:
UIImage *image = [self.delegate mapViewController:self imageForAnnotation:aView.annotation];

当我调试此行时,尽管我将TableViewController关联为其委托,但我仍可以看到self.delegate为null。

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

最佳答案

您的代码都没有显示您将任何内容设置为任何其他内容的委托。您声明了一些属性,并采用了一些协议,但是没有一个协议实际上将delegateself属性设置为一个值。

09-25 18:44