I’m trying to implement a delegate by implementing it within another classes interface file as follows:
ImageScrollView.h
#import <UIKit/UIKit.h>
@protocol ImageScrollViewDelegate <NSObject>
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
- (void)scrollViewDidZoom:(UIScrollView *)scrollView;
@end
@interface ImageScrollView : UIScrollView <UIScrollViewDelegate>
{
id <ImageScrollViewDelegate> _imageScrollViewDelegate;
}
@property(nonatomic, assign) id <ImageScrollViewDelegate> imageScrollViewDelegate;
@end
ImageScrollView.m
#import "ImageScrollView.h"
@implementation ImageScrollView
@synthesize imageScrollViewDelegate = _imageScrollViewDelegate;
...
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[self.imageScrollViewDelegate scrollViewDidScroll:scrollView];
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView;
{
[self.imageScrollViewDelegate scrollViewDidZoom:scrollView];
}
...
@end
and then I have a viewController that implements the ImageScrollViewDelegate:
ViewControllerSubClass.h
#import <UIKit/UIKit.h>
@protocol ImageScrollViewDelegate;
@interface ViewControllerSubClass : UIViewController <ImageScrollViewDelegate> //warning is here
{
}
@end
ViewControllerSubClass.m
#import "ViewControllerSubclass.h"
#import "ImageScrollView.h"
@implementation ViewControllerSubClass
- (void)loadView
{
...
[[self scrollView] setImageScrollViewDelegate:self];
...
- (void) scrollViewDidScroll:(UIScrollView *)scrollView
{
...
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
...
}
In the @interface @interface ViewControllerSubClass : UIViewController <ImageScrollViewDelegate> in ViewControllerSubclass.h, I’m getting the warning: “cannot find the protocal definition for “ImageScrollViewDelegate” four times, yet the code is still working.
Anyone know how to get rid of the warning, or how to properly implement the delegate (better to use separate file?).
You can not forward declare a protocol that you are implementing. You need to
#importImageScrollView.hrather than declare@protocol ImageScrollViewDelegate. The reason it works is because the method exists in your implementation when it is called at runtime, and the reason for the warnings is because the compiler does not know what methods are in the protocol at compile time.