iOS 5.0警告:找不到Delegate的协议定义 [英] iOS 5.0 Warning: Cannot find protocol definition for Delegate
问题描述
我有自定义UIView类GestureView。我有这个课程的前瞻声明,下面是代表。我在.m文件中导入了GestureView.h。这样可以正常工作,但iOS会发出警告消息无法找到GestureViewDelegate的协议定义。如果我删除前向声明,它会给出与错误相同的警告消息。我不想从ContainerViewController.h导入GestureView.h,因为我通常在.m文件中导入东西。有人可以解释下面的类结构有什么问题吗?
I have custom UIView class GestureView. I have a forward declaration for this class and it's delegate below. I have imported GestureView.h in .m file. This works fine but iOS gives warning message saying "Cannot find protocol definition for GestureViewDelegate". If I remove forward declaration it gives same warning message as error. I don't want to import GestureView.h from ContainerViewController.h as I usually imports stuffs in .m file. Could someone please explain what's wrong in following class structure?
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
协议,所以你应该移动它进入你的实现文件。像这样:
I like that you're trying to avoid imports in header files: very good practice. However, to fix your bug you can just make your code even better! In my opinion it's not really necessary that your ContainerViewController
class outwardly declares that it supports GestureViewDelegate
protocol, so you should move this into your implementation file. Like so:
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
这篇关于iOS 5.0警告:找不到Delegate的协议定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!