为自定义协议设置委托会干扰 UIScrollViewDelegate 方法
我使用的是 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");
}
I'm using a custom subclass of UIScrollView
called ImageScrollView
. I need to know when the user taps the scroll view, so I created a protocol. I implement the protocol in my RootViewController
and everything looks ok. When I build it, theres a warning
Class 'ImageScrollView' does not implement the 'TapDetectingImageViewDelegate' protocol".
After inspecting the init
method in the ImageScrollView
class, I see that self.delegate = self;
is causing the problem. I declare myself the delegate for the UIScrollView
delegate methods, but I also am the delegate for my own protocol (Delegate for my protocol must be RootViewController
, not 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;
}
Do you guys know how can I solve that? Or a better idea to tell my RootViewController
that user has tapped the UIScrollView
/UIImage
.
Declaration of protocol in ImageScrollView.h
@protocol TapDetectingImageViewDelegate <NSObject>
@optional
- (void)tapDetectingImageView:(ImageScrollView *)view gotSingleTapAtPoint:(CGPoint)tapPoint;
@end
Header file of class that implements it
#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
Protocol method in .m class that implements it
-(void)tapDetectingImageView:(ImageScrollView *)view gotSingleTapAtPoint:(CGPoint)tapPoint{
NSLog(@"SingleTap");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望视图控制器成为其委托,则不要在初始化程序中设置委托。当您创建
ImageScrollView
实例时,您应该将其设置为视图控制器。如果您通过 IB 创建它,即将
UIScrollView
的类名重命名为ImageScrollView
,则必须为其创建一个出口,然后在UIScrollView
中设置委托。视图控制器的 code>viewDidLoad 方法。然而,这种方法有一个问题。顾名思义,如果
ImageScrollView
是UIScrollView
的子类,则滚动视图的delegate
属性将丢失。您也应该考虑到这一点。If you want the view controller to be its delegate, then don't set the delegate within the initializer. When you're creating the
ImageScrollView
instance, you should set it to the view controller.If you are creating it this via IB i.e. renaming the class name of a
UIScrollView
toImageScrollView
, you will have to create an outlet for it and then set the delegate in theviewDidLoad
method of the view controller.However, there is an issue with this approach. If
ImageScrollView
is the subclass of aUIScrollView
as the name suggests, thedelegate
property of the scroll view is lost. You should take that into consideration as well.您将在
ImageScrollView
的.h文件中声明RootViewController
委托,将根委托的名称更改为
rootDelegate< /代码>;
在ImageScrollView.h中
在ImageScrollView.m文件中
You will be having an declaration of
RootViewController
delegate in the .h file ofImageScrollView
,Change the name of root delegate to
rootDelegate
;In ImageScrollView.h
in ImageScrollView.m file