objective-c - iOS 5.0 警告 : Cannot find protocol definition for Delegate

标签 objective-c ios5 protocols forward-declaration

我有自定义 UIView 类 GestureView。我有一个此类的前向声明,它是下面的委托(delegate)。我在 .m 文件中导入了 GestureView.h。这工作正常,但 iOS 给出警告消息说“找不到 GestureViewDelegate 的协议(protocol)定义”。如果我删除前向声明,它会给出与错误相同的警告消息。我不想从 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

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

关于objective-c - iOS 5.0 警告 : Cannot find protocol definition for Delegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12892463/

相关文章:

c - ASN.1 编译器

objective-c - 获取Xcode 4.5.2 10.6 Base SDK

ios - UIActionSheet showFromRect 自动旋转

ios - 未创建简单导航 Controller

language-agnostic - 在 TCP 之上是否有针对高吞吐量和低延迟进行优化的协议(protocol)/标准?

FFmpeg 从 udp 复制到 rtsp

objective-c - 何时关闭 NSOutputStream?

objective-c - BarButtonItem 因未知原因调整大小

objective-c - 在基于文档的应用程序中连接菜单项

objective-c - 如何在 Storyboard 中的任何 ViewController 之外设计单独的 UIView?