目标C和C中的循环进口问题Cocoa Touch [英] Circular import issues in Objective C & Cocoa Touch
问题描述
我在Cocoa Touch中有一个视图控制器,它检测设备何时旋转,并在其所具有的2个视图控制器的视图之间切换:横向和纵向。
要让 UIViewControllers
能够访问 FRRRotatingViewController
,方法与所有 UIViewControllers
可以访问它们所在的 UINavigationController
。
UIViewController
将具有 rotationViewController
属性的子类( FRRViewController
。
我还修改了 FRRRotatingViewController
,所以它需要 FRRViewControllers
FRRRotatingViewController.h
FRRViewController.h
(反之亦然),我似乎进入一个循环导入问题。我不知道如何解决它。有任何建议吗? 以下是代码:
//
// FRRViewController.h
#import< UIKit / UIKit.h>
#importFRRRotatingViewController.h
@interface FRRViewController:UIViewController
@end
//
FRRRotatingViewController.h
#import< UIKit / UIKit.h>
#importFRRViewController.h
@class FRRRotatingViewController;
@protocol FRRRotatingViewControllerDelegate
- (void)FRRRotatingViewControllerWillSwitchToLandscapeView:(FRRRotatingViewController *)sender;
- (void)FRRRotatingViewControllerWillSwitchToPortraitView:(FRRRotatingViewController *)sender;
@end
@interface FRRRotatingViewController:FRRViewController {
//这是我得到的错误:找不到接口声明
//'FRRViewController','FRRRotatingViewController'的超类;你是
//意思是UIViewController吗?
}
@property(strong)UIViewController * landscapeViewController;
@property(strong)UIViewController * portraitViewController;
@property(unsafe_unretained)id< FRRRotatingViewControllerDelegate>代表;
- (FRRRotatingViewController *)initWithLandscapeViewController:(UIViewController *)landscape andPortraitViewController:(UIViewController *)portrait;
- (void)deviceDidRotate:(NSNotification *)aNotification;
@end
可以在大多数情况下在头部中使用类和协议的前向声明,以避免循环导入问题,除了继承的情况。在 FRRViewController.h
中,不能导入 FRRRotatingViewController.h
,您不能进行向前声明吗?
@class FRRRotatingViewController;
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
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;
这篇关于目标C和C中的循环进口问题Cocoa Touch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!