2016-03-13 39 views
0

如何在Parent中实现Child的代表?在父级实现子委托?

Parent.h:

@interface Parent : NSObject 

Child.h

#import "Parent.h" 
@protocol ChildDelegate <NSObject> 
- (void)someMethod; 
@end 

@interface Child : Parent 

我不能声明父的接口为:

@interface Parent : NSObject<ChildDelegate> 

,因为它需要进口"Child.h",它将被循环输入。

我该如何解决这个问题?

+1

将'ChildDelegate'移动到一个新的头文件中,比如''ChildDelegate.h'''。 – Pang

+0

你能举个例子吗? – Rendy

+0

它是一个单独的文件,并且该文件必须由父母和孩子导入? – Rendy

回答

1

您应该声明源文件中的协议一致性(使用.m扩展名)。

您可以在Parent.h中声明Parent类,但不符合ChildDelegate协议。

@interface Parent : NSObject 

而在您的Parent.m文件中,您可以编写如下内容。

#import "Child.h" 

@interface Parent() <ChildDelegate> 

@end 

@implementation Parent 
// Your implementation code here 
@end