2013-03-09 105 views
3

我正在使用XCode 4.5.1构建iOS 6应用程序为什么我的委托为空?

我试图使用我的控制器可用于检索图像的协议。然而,我的委托从来不叫我,尽管调用它是这样的:

UIImage *image = [self.delegate mapViewController:self imageForAnnotation:aView.annotation]; 

当调试这条线,我可以看到self.delegate为null,尽管我是其委托关联一个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 
+2

你在哪里设置委托? – Etan 2013-03-09 22:52:44

+1

我看不到委托任务。 – CodaFi 2013-03-09 22:52:45

+3

你确实不设置委托。顺便说一句,你可以离开'@synthesize委托= _delegate;'自Xcode 4.4以来。编译器会处理它 – s1m0n 2013-03-09 22:57:41

回答

5

你的代码中没有显示您设置什么作为其他任务的代表。您声明了一些属性,并且您采用了一些协议,但实际上没有一个将delegate属性self设置为某个值。

+0

感谢这么快速的回应马特。 – seedhead 2013-03-09 23:33:45

0

当然,我们没有所有的源代码,但是Etan和CodaFi看起来是正确的:你实例化你的RecentPhotosTableViewController对象并将该对象设置为你的MapViewController对象委托的代码行在哪里?

我认为,我们期待看到类似:

@implementation RecentPhotosTableViewController 

- (void) someMethod { 
    self.mapViewController = [[MapViewController alloc] init]; 
    self.mapViewController.delegate = self; 
    ... 
} 

你已经宣布在的MapViewController类的委托财产,但你实际设置该委托财产的一些对象实例化它后使委托呼叫工作。

(很抱歉,如果这是简单的,但似乎你有一个非常基本的概念上的困难;道歉,如果我错过了点!)

相关问题