To support zooming a image in a UIScrollView, I thought I have to conform the MyController to the UIScrollViewDelegate like as follows. But it works fine without conforming to the UIScrollViewDelegate, even though compiler generates a following warning message.
warning: class ‘MyController’ does not implement the ‘UIScrollViewDelegate’ protocol
Isn’t it mandatory to conform to the UIScrollViewDelegate in a header file?
Are there any side-effects if I forget to conform?
//@interface MyController : UIViewController <UIScrollViewDelegate>
@interface MyController : UIViewController
{
UIScrollView *scrollView;
UIImageView *imageView;
}
@end
@implementation HelloController
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imageView;
}
- (void)loadView
{
UIImage *img = [UIImage imageNamed:@"foo.png"];
imageView = [[UIImageView alloc] initWithImage:img];
imageView.userInteractionEnabled = NO;
scrollView = [[UIScrollView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]];
[scrollView setScrollEnabled:YES];
[scrollView setContentSize:[imageView size]];
[scrollView setMaximumZoomScale:2.0f];
[scrollView setMinimumZoomScale:0.5f];
[scrollView setDelegate:self];
[scrollView addSubview:imageView];
self.view = scrollView;
[scrollView release];
}
@end
If your class actually implements the delegate methods, you won’t get any errors because they will be actually called when required.
Conforming to the protocol just tells the compiler that this class implements those methods. So by omitting it, the compiler will generate a warning because you assign the
UIScrollView.delegateto a class that does not seem to conform to theUIScrollViewDelegateprotocol as required.