0

I have some view controller called mainContainer , he than import some other view controller called myPills, and add it as a subview .

the mainContainer also has a protocol to send delegates to the myPills class, and it looks like :

//mainContainer.h
@protocol mainScrollerDelegate <NSObject>

-(void)function;

@end

@interface MainContainerView : UIViewController<UIScrollViewDelegate>

Than, obviously in the myPills class i cant import the mainContainer view, but i do want to register to the mainContainer delegates.

so in myPills

@interface MyPillsView : UIViewController <mainScrollerDelegate>

will give error on compile .

I have read this,and tried to move the import to be under the delegates-no success.(same error that delegate was undeclared)

Cannot find protocol declaration

How can you listen to a delegate in classB from classA , where classA is importing classB , so classB cant import A back ?

Community
  • 1
  • 1
Curnelious
  • 1
  • 16
  • 76
  • 150

1 Answers1

1

Put your protocol declaration in its own Protocols.h file, for example:

//Protocols.h
@protocol mainScrollerDelegate <NSObject>

-(void)function;

@end

Then, just import it into both the controller who is sending the delegate methods, and the delegate itself:

#import "Protocols.h"

@interface MyPillsView : UIViewController <mainScrollerDelegate>

This method will keep all your protocols organized and can help remove circular imports.

Sidenote: You should really use the class naming convention for your protocol (ie MainScrollerDelegate).

rebello95
  • 8,486
  • 5
  • 44
  • 65
  • so how does the protocols.h should look like ? what kind of file i create in Xcode? and what did you mean by your sidetone ? thanks very much! – Curnelious Jun 20 '15 at 22:11
  • My block of code after `//Protocols.h` is literally all the code needed in that file. Just create a header file in Xcode. My sidenote had a capital `M` instead of a lowercase `m` - it helps distinguish between variables and classes. – rebello95 Jun 20 '15 at 22:12
  • btw where you put the @property(nonatomic,assign) id delegate; ?? – Curnelious Jun 21 '15 at 07:07