Errors when making circular reference imports

2019-01-19 00:37发布

问题:

My program was running fine, but I changed something and now it has over 48 errors.

I think I know the problem, but I don't know how to fix it. I created a class called mViewBase for all my UIViewControllers to derive from.

I decided to have a navigtion bar at the bottom of all my views, to go to other view controllers called cakes2. So cakes2.h imports mViewBase, and mViewBase import cakes2.h

You must be able to do this in Objective-C. Does anybody have any idea of what I can do?

My mViewBase.h file:

#import <UIKit/UIKit.h>
#import "Cakes2.h"

@interface mViewBase : UIViewController {
    UIView *mBackground;
    UIView *mBackArrow;
    UITextView *mTitle;
    //    Cakes2 *mCakes;
}

-(void) aSetTitle: (NSString *) NewTitle;
-(IBAction) aBack: (id) tender;
-(IBAction) aHome: (id) sender;
-(IBAction) aCakes: (id) sender;
-(IBAction) aCall: (id) sender;
-(IBAction) aDirections: (id) sender;
@end

My Cakes2.h file:

#import <UIKit/UIKit.h>
#import "Gallery.h"
#import "WebView.h"
#import "mViewBase.h" // Circular reference! But I need it

@interface Cakes2 : mViewBase <UITableViewDelegate, UITableViewDataSource> {
    //    Gallery *mGallery;
    IBOutlet UITableView *mMenu;
    // WebView *mWebView;
}
-(IBAction) aOpenWeb;
@end

回答1:

You can use a forward declaration in one of your header files to avoid the need to import the other header. For example, in mViewBase.h, you can say:

@class Cakes2;

Now the compiler knows that "Cakes2" refers to a class, and you don't need to import the entire Cakes2.h file.



回答2:

I think you should perhaps consider using a UITabBarController. It is made specifically for managing several view controllers from a bar at the bottom of the screen.