Objective-C:循环协议要求 [英] Objective-C: Circular Protocol Requirements

查看:74
本文介绍了Objective-C:循环协议要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆