0

我对Objective C很陌生,所以请对我慢一点。

我已经像这样构建了我的视图:ViewController=>Root(View)=>Controls(View)。Root 是一个 Singleton,所以我可以随时获取我的应用程序的根元素。

当我添加#import "Root.h"时,Controls.h我遇到了解析问题。

#import <UIKit/UIKit.h>
#import "Root.h"      // triggers the error

@interface Controls : UIView

@end

这是我的 Root.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "Controls.h"
#import "Content.h"

@interface Root : UIView
{
    Controls *m_controls; // Parse error: Unknown type name "Controls"
}
+(Root*)getInstance;
@end

是什么原因造成的?怎么修?

4

2 回答 2

2

您只能在 *.m 文件中使用 #import *.h,当您需要在 *.h 中使用某些类时,您可以使用 @class。

因此,您必须像这样更改您的课程:

#import <UIKit/UIKit.h>
@class Root 
@interface Controls : UIView
@end

在 *.m 文件中:

#import "根.h"

于 2013-05-08T15:53:25.633 回答
1

您有一个导入依赖循环。Root.h 导入 Controls.h,反之亦然。其中一个必须先走,而那个不知道另一个的声明。

一般来说,尽可能多地在实现 (.m) 文件中进行导入,并在 .h 中使用前向声明,如下所示:

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "Content.h"

@class Controls;

@interface Root : UIView
{
    Controls *m_controls;  //Prase error: Unknown type name "Controls"
}
+(Root*)getInstance;
@end

顺便说一句,您现在可以将实例变量@implementation放在@interface

于 2013-05-08T15:31:52.700 回答