开发者

Why I can support zooming a UIScrollView without conforming to the UIScrollViewDelegate?

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


Replace this line:

[scrollView setDelegate:self];

with this:

[scrollView setDelegate:(id<UIScrollViewDelegate>)self];

Still, I don't know why the compiler isn't recognizing that you have implemented UIScrollViewDelegate

edit: to satisfy the compiler, you can also implement the protocol on a private category defined in your .m file:

@interface FormModel () <UIScrollViewDelegate>
@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.delegate to a class that does not seem to conform to the UIScrollViewDelegate protocol as required.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜