【发布时间】:2011-06-08 16:17:38
【问题描述】:
我正在使用 UIScrollView 的自定义子类,称为 ImageScrollView。我需要知道用户何时点击滚动视图,所以我创建了一个协议。我在RootViewController 中实现了该协议,一切看起来都很好。当我构建它时,有一个警告
“ImageScrollView”类未实现“TapDetectingImageViewDelegate”协议”。
检查ImageScrollView 类中的init 方法后,我发现self.delegate = self; 导致了问题。我声明自己是 UIScrollView 委托方法的委托,但我也是我自己协议的委托(我的协议的委托必须是 RootViewController,而不是 ImageScrollView)。
- (id)initWithFrame:(CGRect)frame{
if ((self = [super initWithFrame:frame])) {
self.showsVerticalScrollIndicator = NO;
self.showsHorizontalScrollIndicator = NO;
self.bouncesZoom = YES;
self.decelerationRate = UIScrollViewDecelerationRateFast;
self.delegate = self;
}
return self;
}
你们知道我该如何解决吗?或者更好的办法是告诉我的RootViewController 用户已经点击了UIScrollView/UIImage。
ImageScrollView.h中的协议声明
@protocol TapDetectingImageViewDelegate <NSObject>
@optional
- (void)tapDetectingImageView:(ImageScrollView *)view gotSingleTapAtPoint:(CGPoint)tapPoint;
@end
实现它的类的头文件
#import <UIKit/UIKit.h>
#import "ImageScrollView.h"
@interface AppleScrollViewViewController : UIViewController <UIScrollViewDelegate, TapDetectingImageViewDelegate>{
UIScrollView *pagingScrollView;
}
-(void)configurePage:(ImageScrollView *)page forIndex:(NSUInteger)index;
- (CGRect)frameForPageAtIndex:(NSUInteger)index;
- (CGRect)frameForPagingScrollView;
- (UIImage *)imageAtIndex:(NSUInteger)index;
- (NSString *)imageNameAtIndex:(NSUInteger)index;
- (CGSize)imageSizeAtIndex:(NSUInteger)index;
- (NSArray *)imageData;
@end
.m 类中实现它的协议方法
-(void)tapDetectingImageView:(ImageScrollView *)view gotSingleTapAtPoint:(CGPoint)tapPoint{
NSLog(@"SingleTap");
}
【问题讨论】:
-
你能发布你的协议声明和实现协议的类的 .h 吗?这听起来像是协议中方法声明的签名和实现不匹配。
-
当我发布你问的内容时,注意到我在课堂上实现了错误的协议方法。我实施了正确的方法,它记录了“Single Tap”。但我仍然收到警告。有什么想法吗?
标签: objective-c cocoa-touch ios delegates protocols