Cannot find protocol declaration for
Remove this line from viewController1.h
:
#import "viewController2.h"
The problem is that viewController2
's interface is preprocessed before the protocol declaration.
The general structure of the file should be like this:
@protocol viewController1Delegate;@class viewController2;@interface viewController1@end@protocol viewController1Delegate <NSObject>@end
A.h: #import "B.h" // A @class A; @protocol Delegate_A (method....) @end @interface ViewController : A @property(nonatomic,strong)id<ViewControllerDelegate> preViewController_B;(protocol A) @end B.h: #import "A.h" // A @class B; @protocol Delegate_B (method....) @end @interface ViewController : B @property(nonatomic,strong)id<ViewControllerDelegate> preViewController_A;(protocol B) @end A.m: @interface A ()<preViewController_B> @end @implementation A (implement protocol....) end B.m: @interface B ()<preViewController_A> @end @implementation B (implement protocol....) @end
For those who might need it:
It's also possible to fix this by moving the importation of ViewController1.h in ViewController2's implementation file (.m) instead of the header file (.h).
Like so:
ViewController1.h
#import ViewController2.h@interface ViewController1 : UIViewController <ViewController2Delegate>@end
ViewController2.h
@protocol ViewController2Delegate;@interface ViewController2@end
ViewController2.m
#import ViewController2.h#import ViewController1.h@implementation ViewController2@end
This will fix the case where the error happens because ViewController1.h is imported in ViewController2.h before the protocol declaration.