iOS 5.0警告:找不到Delegate的协议定义

app*_*eak 6 protocols objective-c forward-declaration ios5

我有自定义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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

Ell*_*eal 22

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

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

  • @TRedman我在这种情况下的建议是在一个单独的头文件(例如`GestureViewDelegate.h`)中声明`GestureViewDelegate`,它可以在实现头文件中导入(而类本身可以是前向声明的). (2认同)