我有自定义的UIView类GestureView。我对此类(class)有一个前向声明,下面是其委托(delegate)。我已经在.m文件中导入了GestureView.h。这可以正常工作,但是iOS发出警告消息,提示“找不到GestureViewDelegate的协议(protocol)定义”。如果删除前向声明,它会给出与错误相同的警告消息。我不想从ContainerViewController.h导入GestureView.h,因为我通常在.m文件中导入内容。有人可以解释下面的类(class)结构有什么问题吗?

ContainerViewController.h

#import <UIKit/UIKit.h>

@class DividerView;
@class GestureView;
@protocol GestureViewDelegate;

@interface ContainerViewController : UIViewController<GestureViewDelegate>
   @property (strong, nonatomic) IBOutlet GestureView *topContentView;
@end

GestureView.h
#import <UIKit/UIKit.h>

@protocol GestureViewDelegate;

@interface GestureView : UIView
    - (void)initialiseGestures:(id)delegate;
@end

@protocol GestureViewDelegate <NSObject>
@required
- (void)GestureView:(GestureView*)view handleSignleTap:(UITapGestureRecognizer*)recognizer;
@end

最佳答案

我喜欢您尝试避免在头文件中导入:很好的做法。但是,要修复您的错误,您可以使代码更好!在我看来,您的ContainerViewController类没有必要向外声明它支持GestureViewDelegate协议(protocol),因此您应该将其移到实现文件中。像这样:

GestureView.h

#import <UIKit/UIKit.h>


@protocol GestureViewDelegate;

@interface GestureView : UIView

- (void)initialiseGestures:(id <GestureViewDelegate>)delegate;

@end


@protocol GestureViewDelegate <NSObject>
@required

- (void)gestureView:(GestureView *)view handleSingleTap:(UITapGestureRecognizer *)recognizer;

@end

ContainerViewController.h
#import <UIKit/UIKit.h>


@class GestureView;

@interface CollectionViewController : UIViewController

// this property is declared as readonly because external classes don't need to modify the value (I guessed seen as it was an IBOutlet)
@property (strong, nonatomic, readonly) GestureView *topContentView;

@end

ContainerViewController.m
#import "ContainerViewController.h"
#import "GestureView.h"


// this private interface declares that GestureViewDelegate is supported
@interface CollectionViewController () <GestureViewDelegate>

// the view is redeclared in the implementation file as readwrite and IBOutlet
@property (strong, nonatomic) IBOutlet GestureView *topContentView;

@end


@implementation ContainerViewController

// your implementation code goes here

@end

09-06 19:53