Objective-C:循环协议要求 [英] Objective-C: Circular Protocol Requirements
问题描述
我有 2 个视图控制器、一个对象选择器和一个对象编辑器.这两个对象都有自己的协议以允许演示者接收数据和接收数据.解雇他们.
I have 2 view controllers, an object picker and an object editor. Both of these objects have their own protocols to allow the presenters to receive data & dismiss them.
编辑器有时需要呈现选择器,而选择器有时需要允许编辑对象.
The editor sometimes needs to present a picker, and the picker needs to sometimes allow objects to be edited.
问题在于存在循环协议依赖性,并且由于对象必须符合协议,因此声明转发不起作用(您仍然会收到编译器警告).
The problem is that there is a circular protocol dependancy, and as the objects have to conform to the protocols, declaration forwarding doesn't work (you still get compiler warnings).
我不只是声明需要符合它的 ivar,整个选择器/编辑器类都需要符合,因此标头需要有关协议的完整信息.
I'm not just declaring an ivar that needs to conform to it, the whole picker/editor class needs to conform, thus the headers need the full information on the protocols.
我收到一个 找不到EditorDelegate"的协议定义
错误.
这是一个例子:
Picker.h
#import "Editor.h"
@protocol PickerDelegate;
@interface Picker : UIViewController <EditorDelegate> {
id <PickerDelegate> delegate;
}
@protocol PickerDelegate <NSObject>
- (void)picker:(Picker *) wasDismissedWithObject:(id)object;
@end
Editor.h
#import "Picker.h"
@protocol EditorDelegate;
@interface Editor : UIViewController <PickerDelegate> {
id <EditorDelegate> delegate;
}
@protocol EditorDelegate <NSObject>
- (void)editor:(Editor *) dismissedAfterEditingObject:(id)object;
@end
如何克服这个问题?
推荐答案
您不能在单独的文件中定义协议,然后将它们导入到 .m 文件中吗?
Can't you define the protocols in separate files, and import them in the .m files?
这篇关于Objective-C:循环协议要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!