0

I'm trying to add this to my code

@implementation UIWebView(CustomScroll)
- (void) scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.delegate scrollViewDidScroll: scrollView];
}
@end

But getting "No known instance method for selector 'scrollViewDidScroll:'" What am I missing here?

Community
  • 1
  • 1
Segev
  • 19,035
  • 12
  • 80
  • 152

1 Answers1

3

The UIWebViewDelegate protocol does not declare a method scrollViewDidScroll:. Hence, this is why you're getting the bad access error.

I'd recommend that you subclass UIWebView instead of trying to creating a category on it (yes, the docs recommend against subclassing... just don't override any of the methods it has, and it's safe).

On your subclass, you can either create a new protocol or simply overwrite the delegate property to also require that it conforms to `UIScrollViewDelegate (the later is shown below):

In Example:

// MyWebView.h
#import <UIKit/UIKit.h>

@interface MyWebView : UIWebView
@property (nonatomic, assign) id<UIWebViewDelegate, UIScrollViewDelegate> delegate;
@end


// MyWebView.m
#import "MyWebView.h"

@implementation MyWebView

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    /* As scrollViewDidScroll: is optional, you should check if the super class
       responds to this method (it doesn't appear to now, but this may change in future) */

    if ([[self superclass] instancesRespondToSelector:@selector(scrollViewDidScroll:)])
    {
        [super scrollViewDidScroll:scrollView];
    }

    /* Likewise, you should check if your own delegate responds to this selector */

    if ([self.delegate respondsToSelector:@selector(scrollViewDidScroll:)])
    {
        [self.delegate scrollViewDidScroll:scrollView];
    }
}

@end
JRG-Developer
  • 12,454
  • 8
  • 55
  • 81
  • Please see edited answer with further clarification. Let me know if you still need help. :D – JRG-Developer Mar 22 '13 at 00:16
  • Thanks for the help. When I'm trying to scroll the subclassed `UIWebView` I'm getting `-[MyWebView scrollViewDidScroll:]: unrecognized selector sent to instance 0x819cf50` – Segev Mar 22 '13 at 01:02
  • for some reason `[super scrollViewDidScroll:scrollView];` is failing? – Segev Mar 22 '13 at 01:08
  • Because as far as the compiler knows, `UIWebView` is not a `UIScrollViewDelegate`. You should be able to work around this with `@interface UIWebView(Foo) @end`, but it's a little bit iffy (as long as you never call it yourself you should be fine, though). – tc. Mar 22 '13 at 03:51
  • @tc, actually the compiler does know... see `UIWebView.h` which shows `@interface UIWebView : UIView `. Hence, there's no need to declare that your subclass conforms to `UIScrollViewDelegate` twice. – JRG-Developer Mar 22 '13 at 06:46
  • Please see the edited answer. Apparently, while `UIWebView` conforms to `UIScrollViewDelegate`, it does not implement `scrollViewDidScroll:`. However, as this may change in future (it's implementation isn't public), you should check to see if it does... see comments in code regarding such. :) – JRG-Developer Mar 22 '13 at 06:48
  • @JRG-Developer D'oh. Ignore my previous comment; the logic is completely flawed! – tc. Mar 22 '13 at 15:57