为自定义协议设置委托会干扰 UIScrollViewDelegate 方法

发布于 2024-11-14 06:21:37 字数 2037 浏览 3 评论 0原文

我使用的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

战皆罪 2024-11-21 06:21:37

如果您希望视图控制器成为其委托,则不要在初始化程序中设置委托。当您创建 ImageScrollView 实例时,您应该将其设置为视图控制器。

ImageScrollView * aScrollView = [[ImageScrollView alloc] initWithFrame:aFrame];
aScrollView.delegate = self;
[..]

如果您通过 IB 创建它,即将 UIScrollView 的类名重命名为 ImageScrollView,则必须为其创建一个出口,然后在 UIScrollView 中设置委托。视图控制器的 code>viewDidLoad 方法。

然而,这种方法有一个问题。顾名思义,如果 ImageScrollViewUIScrollView 的子类,则滚动视图的 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.

ImageScrollView * aScrollView = [[ImageScrollView alloc] initWithFrame:aFrame];
aScrollView.delegate = self;
[..]

If you are creating it this via IB i.e. renaming the class name of a UIScrollView to ImageScrollView, you will have to create an outlet for it and then set the delegate in the viewDidLoad method of the view controller.

However, there is an issue with this approach. If ImageScrollView is the subclass of a UIScrollView as the name suggests, the delegate property of the scroll view is lost. You should take that into consideration as well.

笑脸一如从前 2024-11-21 06:21:37

您将在ImageScrollView.h文件中声明RootViewController委托,

将根委托的名称更改为rootDelegate< /代码>;

在ImageScrollView.h中

  RootViewControllerDelegate* rootDelegate;
  @property(nonatomic,assign) RootViewControllerDelegate* rootDelegate;

在ImageScrollView.m文件中

@synthesize rootDelegate;
- (id)initWithFrame:(CGRect)frame{
      if ((self = [super initWithFrame:frame])) {
         self.showsVerticalScrollIndicator = NO;
         self.showsHorizontalScrollIndicator = NO;
         self.bouncesZoom = YES;
         self.decelerationRate = UIScrollViewDecelerationRateFast;
         self.delegate = self;    
         // Root controller delegate.
         self.rootDelegate = self;            
      }
      return self;
 }

You will be having an declaration of RootViewController delegate in the .h file of ImageScrollView,

Change the name of root delegate to rootDelegate ;

In ImageScrollView.h

  RootViewControllerDelegate* rootDelegate;
  @property(nonatomic,assign) RootViewControllerDelegate* rootDelegate;

in ImageScrollView.m file

@synthesize rootDelegate;
- (id)initWithFrame:(CGRect)frame{
      if ((self = [super initWithFrame:frame])) {
         self.showsVerticalScrollIndicator = NO;
         self.showsHorizontalScrollIndicator = NO;
         self.bouncesZoom = YES;
         self.decelerationRate = UIScrollViewDecelerationRateFast;
         self.delegate = self;    
         // Root controller delegate.
         self.rootDelegate = self;            
      }
      return self;
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文