Objective C 中的循环导入问题可可触感
我在 Cocoa Touch 中有一个视图控制器,它可以检测设备何时旋转并在其具有的 2 个视图控制器的视图之间切换:横向和纵向。
我希望其中的 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
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.
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.
So I created a UIViewController
subclass (FRRViewController
) that will have a rotatingViewController
property.
I also modified FRRRotatingViewController
, so it takes FRRViewControllers
instead of plain UIViewControllers
.
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?
Here's the code:
//
// 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在大多数情况下,您可以在标头中使用类和协议的前向声明,以避免循环导入问题(继承的情况除外)。在
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 importingFRRRotatingViewController.h
, can you not make a forward declaration?如果我没看错的话,你的继承结构是这样的:
UIViewController-->FRRViewController-->FRRRotatingViewController
但是你已经使 FRRViewController 的声明依赖于从其子类 FRRotatingViewController 导入文件。因此,编译器将在处理 FRRViewController.h 的其余部分之前导入并读取 FRRRotatingViewController。
我假设您错过了一些 FRRViewController.h,因为其中没有引用任何旋转视图控制器,但如果您在 FRRRotatingViewController 的 .h 中声明属性,那么您需要在 FRRViewController 中使用前向声明.h:
而不是后一行。
您可以放入
FRRViewController.m
文件的第一行,If I'm reading it right your inheritance structure is like so:
UIViewController-->FRRViewController-->FRRRotatingViewController
But you have made the declaration of FRRViewController dependent on importing the file from its subclass, FRRRotatingViewController. The compiler will therefore be importing and reading FRRRotatingViewController before it processes the rest of FRRViewController.h.
I'm assuming you've missed out some of FRRViewController.h since there is no reference to any rotating view controllers in there, but if you are declaring a property in .h of FRRRotatingViewController, then you need to use a forward declaration in FRRViewController.h:
instead of
The latter line, you can put in the first line of your
FRRViewController.m
file.