9

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
jscs
  • 63,694
  • 13
  • 151
  • 195
Ted pottel
  • 6,869
  • 21
  • 75
  • 134
  • similar: http://stackoverflow.com/questions/1619075/possible-circular-reference-problem – Marek Sebera Aug 28 '11 at 13:18
  • are you sure that's the problem? According to some poking around I've just done "the #import directive ensures that files are imported (included) just once per compilation" in objective-c. – Dogmatixed Aug 28 '11 at 13:23

2 Answers2

19

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.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • Try and include as few files as possible in.h files, prefer @class forward declarations. A header file is designed to expose the public interface of the class. – zaph Aug 28 '11 at 13:44
  • 6
    Hi this did the trick I put the @class in header file and the import in the m file :) Thank you, Ted – Ted pottel Aug 29 '11 at 16:44
0

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.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Hi thank you for your comment, I was thinking about that, but my project is almost done and did not want to tear up the code. – Ted pottel Aug 29 '11 at 16:45