【发布时间】:2011-09-20 19:30:30
【问题描述】:
我有两个view controllers A 和B,他们都有彼此作为他们的代表。
当我除了在头文件的开头定义协议和#import另一个头文件之外什么都不做时,我遇到了两个错误,类似于 -
找不到“BDelegate”的协议声明,它显示在 A.h(我写的地方)找不到协议声明 “ADelegate”,显示在 B.h(我写的地方)
查看网上,人们之前曾写过头文件的循环包含可能会导致问题。他们建议使用 #include 或 @class 声明,如 -
@class A
而不是
#import A.h
在#import B.h内
我已经尝试了这些导入的几乎所有组合,@classes 和 #include,但仍然无法摆脱警告。此外,在线解决方案建议将#import 移动到.m 文件,但这也无济于事。部分原因是网上的解决方案有点模糊——如果你能把它分解,那就太好了。
有什么建议可以解决这个问题吗?
-- BigViewController.h --
#import "BaseViewController.h"
#include "BaseViewController.h"
@class BigViewController;
@protocol BigViewControllerDelegate
-(void) BigViewController:(BigViewController *) bigView;
@end
@interface BigViewController : UIViewController <BaseViewControllerDelegate>
{
//delegate
id <BigViewControllerDelegate> delegate;
ivars...
}
@properties...
@end
--------------------------------------------------
-- BaseViewController.h --
#<UIKit/UIKit.h>
#import "BigViewController.h"
#include "BigViewController.h"
@class BigViewController;
@protocol BaseViewControllerDelegate
- (void) setParametersWithItemChosen:(Item *) item;
@end
@interface BaseViewController : UIViewController <...BigViewControllerDelegate...>
{
ivars...
//delegate
id <BaseViewControllerDelegate> delegate;
}
@properties...
@end
【问题讨论】:
-
也许您可以发布一些相关代码(精简以显示您定义的内容)。
-
我刚刚发布了一些。缩进很奇怪 - 抱歉!
-
以后可以使用顶部的
{}按钮来封装代码,或者使用反引号手动格式化。 -
按照@TwistedUmbrella 的说明,我遇到了同样的问题并解决了。
-
检查您包含的文件。
这是两个相互包含的标头的症状。
有时会导致此错误。
标签: objective-c cocoa-touch protocols