我在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
要在B中导入A,在B.h中添加@class A,并在B.m.中添加#import“A.h”。同样的东西在A中导入B. – Jano
为什么不用一个新的实现(例如FernardosViewController)来为UIViewController子类化,这个实现包含了你希望在子类之间通用的任何方法?然后您将拥有FRRRotatingViewController和FRRViewController从FernardosViewController派生出来,并且您可以在.h文件中保持对彼此的引用(这将消除您的循环导入问题)。 –
在FRRViewController中向FRRRotatingViewController添加一个前向声明,并在FRRViewController.h中导入FRRViewController解决了它。编译器不会接受一个超类,这是一个前向声明。 – cfischer