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 !
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.hand 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