Objective C & 中的循环导入问题可可触摸 [英] Circular import issues in Objective C & Cocoa Touch
问题描述
我在 Cocoa Touch 中有一个视图控制器,用于检测设备何时旋转并在它拥有的 2 个视图控制器的视图之间切换:横向和纵向.
I have a view controller in Cocoa Touch that detects when the device rotates and switches between the views of the 2 view controllers it has: landscape and portrait.
我希望其中的 UIViewControllers
能够访问 FRRRotatingViewController
,就像所有 UIViewControllers
都可以访问 >UINavigationController
他们在.
I want the UIViewControllers
in it to be able to access the FRRRotatingViewController
, in a similar way as all UIViewControllers
can access the UINavigationController
they're in.
所以我创建了一个 UIViewController
子类 (FRRViewController
),它将有一个 rotatingViewController
属性.
So I created a UIViewController
subclass (FRRViewController
) that will have a rotatingViewController
property.
我还修改了 FRRRotatingViewController
,所以它需要 FRRViewControllers
而不是普通的 UIViewControllers
.
I also modified FRRRotatingViewController
, so it takes FRRViewControllers
instead of plain UIViewControllers
.
不幸的是,当我在 FRRViewController.h
中包含 FRRRotatingViewController.h
时(反之亦然),我似乎陷入了循环导入问题.我不知道如何修复它.有什么建议吗?
Unfortunately, when I include FRRRotatingViewController.h
in FRRViewController.h
(and vice versa), I seem to get into a circular import issue. I don't know how to fix it. Any suggestions?
代码如下:
//
// 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
推荐答案
在大多数情况下,您可以在头文件中使用类和协议的前向声明来避免循环导入问题,继承的情况除外.在FRRViewController.h
中,不导入FRRRotatingViewController.h
,可以不做前置声明吗?
You can use forward declarations for classes and protocols in headers in most situations to avoid circular import issues, except in the case of inheritance. In FRRViewController.h
, instead of importing FRRRotatingViewController.h
, can you not make a forward declaration?
@class FRRRotatingViewController;
这篇关于Objective C & 中的循环导入问题可可触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!