0

I was building an app when I ran into some errors. After doing some research, I found that the reason is because I am working with 2 files, that each #import each other. I read that the cure to this is to use Forward Declaration, but I couldn't find a good example of how this is done.

Here is what I have.

RootViewController.h

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

@protocol SubstitutableDetailViewController
- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem;
- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem;
@end


@interface RootViewController : UITableViewController <UISplitViewControllerDelegate, FirstDetailViewControllerDelegate>{

    UISplitViewController *splitViewController;

    UIPopoverController *popoverController;    
    UIBarButtonItem *rootPopoverButtonItem;

    NSMutableArray *logMessages;
}

@property (nonatomic, assign) IBOutlet UISplitViewController *splitViewController;

@property (nonatomic, retain) UIPopoverController *popoverController;
@property (nonatomic, retain) UIBarButtonItem *rootPopoverButtonItem;

@end

FirstViewDetailController.h

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

//test2
@protocol FirstDetailViewControllerDelegate <NSObject>
- (void)addItemViewController:(FirstDetailViewController *)controller didFinishEnteringItem:(NSString *)item;
@end
//end test2

@interface FirstDetailViewController : UIViewController <SubstitutableDetailViewController> {

    //for the output
    IBOutlet UITextView *outputView;

    UIToolbar *navigationBar;

}

@property (nonatomic, retain) IBOutlet UIToolbar *navigationBar;

//test
@property(nonatomic, retain) NSString *message;
//end test

@property (nonatomic, retain) id <FirstDetailViewControllerDelegate> delegate;

@end

I know that I need to replace #import with @class, but do I do it for both occurrences? Also, I a already #import "FirstDetailViewController.h" in the RootViewController.m file, so do I switch it there as well ?

I am a little confused so any help would be appreciated !

BloonsTowerDefence
  • 1,184
  • 2
  • 18
  • 43

2 Answers2

1

Forward declaration would be the solution if it were just pointers you needed, but you are implementing the other header's protocol in each class.

My suggestion would be to declare your protocols in some other header file, such as MyProtocols.h and include that in both of your .h files instead of the controllers' headers.

On a side note, having a strong, or retained reference to one's delegate isn't really standard practice, as this can easily cause a retain cycle which leads to leaked memory

Dan F
  • 17,654
  • 5
  • 72
  • 110
  • The reason I am implementing the protocols is because I am trying to pass values from one class to another. I was following the first answer in [THIS](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) question, because apparently that is the correct way to do it. – BloonsTowerDefence Jul 10 '12 at 18:58
  • Thats all fine, you just need to move the protocol declarations to a new file – Dan F Jul 10 '12 at 19:00
  • I got it all working now (I did like you said and made the Protocols.h file), thanks again – BloonsTowerDefence Jul 10 '12 at 20:36
1

Replace

#import "FirstDetailViewController.h" with

@class FirstDetailViewController;


Replace

#import "RootViewController.h" with

@class RootViewController;


in RootViewController.m, make sure you have

#import "RootViewController.h"

#import "FirstDetailViewController.h"

in FirstDetailViewController.h make sure you have

#import "FirstDetailViewController.h"

#import "RootViewController.h"

Edit: Oops missed the protocol references... Dan F's answer is correct

DanSkeel
  • 3,853
  • 35
  • 54
  • The compiler needs to know about the protocols that are being implemented on each class, not the existence of each class itself – Dan F Jul 10 '12 at 18:59