6

我有自定义 UIView 类 GestureView。我有这个类的前向声明,它是下面的代表。我在 .m 文件中导入了 GestureView.h。这工作正常,但 iOS 发出警告消息说“找不到 GestureViewDelegate 的协议定义”。如果我删除前向声明,它会给出与错误相同的警告消息。我不想从 ContainerViewController.h 导入 GestureView.h,因为我通常会在 .m 文件中导入东西。有人可以解释一下类结构有什么问题吗?

ContainerViewController.h

#import <UIKit/UIKit.h>

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

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

手势视图.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
4

2 回答 2

22

我喜欢你试图避免在头文件中导入:非常好的做法。但是,要修复您的错误,您可以使您的代码变得更好!在我看来,你的类没有必要ContainerViewController对外声明它支持GestureViewDelegate协议,所以你应该把它移到你的实现文件中。像这样:

手势视图.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

容器视图控制器.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
于 2012-10-15T20:48:40.537 回答
0

试试这个方法,如果有效,请回复。

手势视图.h

#import <UIKit/UIKit.h>

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

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

容器视图.h

#import <UIKit/UIKit.h>

@class DividerView;
@class GestureView;
/*@protocol GestureViewDelegate;*/ //NO NEED TO WRITE THIS

@interface ContainerViewController : UIViewController<GestureViewDelegate>
   @property (strong, nonatomic) IBOutlet GestureView *topContentView;
@end
于 2012-10-15T12:22:43.120 回答