Objective C 中的循环导入问题可可触感

发布于 2024-12-10 01:08:45 字数 1887 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一枫情书 2024-12-17 01:08:45

在大多数情况下,您可以在标头中使用类和协议的前向声明,以避免循环导入问题(继承的情况除外)。在FRRViewController.h中,不导入FRRRotatingViewController.h,可以不做前向声明吗?

@class FRRRotatingViewController;

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;
昵称有卵用 2024-12-17 01:08:45

如果我没看错的话,你的继承结构是这样的:

UIViewController-->FRRViewController-->FRRRotatingViewController

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

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

@class FRRRotatingViewController

而不是后一行。

#import "FRRRotatingViewController.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:

@class FRRRotatingViewController

instead of

#import "FRRRotatingViewController.h"

The latter line, you can put in the first line of your FRRViewController.m file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文