自定义代理不接收呼叫

时间:2012-12-20 17:14:37

标签: iphone objective-c ios delegates

我的自定义代表未接听电话。这是我的设置。 ViewController有一个SliderView对象,它是UIScrollView的子类:

SlideView.h

#import <UIKit/UIKit.h>

@protocol SlideViewDelegate <NSObject
@required
-(void) didTapImageData:(NSMutableArray*)imageData atIndex:(int)index;
@end

@interface SliderView : UIScrollView<UIGestureRecognizerDelegate> {
   __weak id<SlideViewDelegate> slideViewDelegate;
}

@property (nonatomic, weak) id<SlideViewDelegate> slideViewDelegate;

@end

SlideView.m

#import "SliderView.h"
@implementation SliderView
@synthesize slideViewDelegate;

- (void) handleTap:(UITapGestureRecognizer *)recognizer{
    NSLog(@"tapped");
   [[self slideViewDelegate] didTapImageData: imageData atIndex:0 ];
}

ViewController.h

@interface ViewController : UIViewController <
                                        UIScrollViewDelegate,
                                        SlideViewDelegate>{
   SliderView *thumbGalleryView;//delegate and reference are set in XCode
}

ViewController.m

#import "ViewController.h"
@implementation ViewController

-(void)didTapImageData:(NSMutableArray*) imageData atIndex:(int)index{
    NSLog(@"NOT WORKING HERE");
}

所以ViewController永远不会收到上述方法的电话。 thumbGalleryViewViewController相关联,委托也设为ViewControllerSlideView的{​​{1}}正在打印邮件正常但handleTap被忽略。为什么?

2 个答案:

答案 0 :(得分:1)

仔细检查您设置代理的位置 - 如果您的代理人nil -handleTap:调用您的-didTapImageData:atIndex:方法,则不会发生任何事情。

答案 1 :(得分:1)

您已设置滚动视图为delegate的{​​{1}}。

您必须将ivar设置为slideViewDelegate


<强>被修改

添加ViewController

IBOutlet

然后从你的xib连接IBOutlet __weak id<SlideViewDelegate> slideViewDelegate; slideViewDelegate
另请记住将滚动视图的类更改为ViewController
为清晰起见添加了图像 enter image description here