2011-10-14 52 views
4

我在Cocoa Touch中有一个视图控制器,用于检测设备何时旋转并在它具有的2个视图控制器的视图之间切换:横向和纵向。目标C和可可触摸中的循环导入问题

我想在它的UIViewControllers能够访问FRRRotatingViewController,以类似的方式,因为所有UIViewControllers可以访问他们所在的UINavigationController

所以我创建了一个UIViewController子类(FRRViewController),将有一个rotatingViewController属性。

我也修改了FRRRotatingViewController,所以需要FRRViewControllers而不是普通的UIViewControllers

不幸的是,当我在FRRViewController.h(反之亦然)中包含FRRRotatingViewController.h时,我似乎陷入了循环导入问题。我不知道如何解决它。有什么建议么?

下面的代码:

// 
// FRRViewController.h 

#import <UIKit/UIKit.h> 
#import "FRRRotatingViewController.h" 

@interface FRRViewController : UIViewController 

@end 

// 
// FRRRotatingViewController.h 

#import <UIKit/UIKit.h> 
#import "FRRViewController.h" 

@class FRRRotatingViewController; 


@protocol FRRRotatingViewControllerDelegate 

-(void) FRRRotatingViewControllerWillSwitchToLandscapeView: (FRRRotatingViewController *) sender; 
-(void) FRRRotatingViewControllerWillSwitchToPortraitView: (FRRRotatingViewController *) sender; 

@end 


@interface FRRRotatingViewController : FRRViewController { 
    // This is where I get the error:Cannot find interface declaration for 
    // 'FRRViewController', superclass of 'FRRRotatingViewController'; did you 
    // mean 'UIViewController'? 
} 

@property (strong) UIViewController *landscapeViewController; 
@property (strong) UIViewController *portraitViewController; 

@property (unsafe_unretained) id<FRRRotatingViewControllerDelegate> delegate; 

-(FRRRotatingViewController *) initWithLandscapeViewController: (UIViewController *) landscape andPortraitViewController: (UIViewController *) portrait; 
-(void) deviceDidRotate: (NSNotification *) aNotification; 

@end 
+1

要在B中导入A,在B.h中添加@class A,并在B.m.中添加#import“A.h”。同样的东西在A中导入B. – Jano

+1

为什么不用一个新的实现(例如FernardosViewController)来为UIViewController子类化,这个实现包含了你希望在子类之间通用的任何方法?然后您将拥有FRRRotatingViewController和FRRViewController从FernardosViewController派生出来,并且您可以在.h文件中保持对彼此的引用(这将消除您的循环导入问题)。 –

+0

在FRRViewController中向FRRRotatingViewController添加一个前向声明,并在FRRViewController.h中导入FRRViewController解决了它。编译器不会接受一个超类,这是一个前向声明。 – cfischer

回答

16

您可以使用正类和协议,声明在标题在大多数情况下,避免圆形的导入问题,除了在继承的情况下。在FRRViewController.h中,您是否可以不进行前向声明,而不是导入FRRRotatingViewController.h

@class FRRRotatingViewController; 
4

如果我读它的权利你的继承结构是像这样:

的UIViewController - > FRRViewController - > FRRRotatingViewController

但是你已经FRRViewController声明依赖进口来自其子类FRRRotatingViewController的文件。因此,编译器将在处理FRRViewController.h的其余部分之前导入并读取FRRRotatingViewController。

我假设你错过了一些FRRViewController.h,因为没有任何旋转视图控制器在那里的引用,但如果你在FRRRotatingViewController的.h中声明了一个属性,那么你需要使用在FRRViewController.h向前声明:中

@class FRRRotatingViewController 

代替

#import "FRRRotatingViewController.h" 

后者线,你可以把你的FRRViewController.m文件的第一行。