views:

768

answers:

4

Hi,

I'm seriously losing my hair because of simple task which turned to hell. So I have UIScrollView subclass - MyScrollView and UIViewController subclass StoryViewController using that scroll view. I need to know when scroll is scrolled so what do I do? I try to delegate!

// StoryViewController.h
@interface StoryViewController : UIViewController <UIScrollViewDelegate>

// StoryViewController.m
// init
scrollView  = [[MyScrollView alloc] init];
scrollView.delegate = self;

// delegated function
- (void)scrollViewDidScroll:(UIScrollView *)s {
    NSLog(@"TEST");
}

Edit: Some NSLogs

Self: <StoryViewController: 0xfce090>
ScrollView Delegate: <StoryViewController: 0xfce090>
WebView Delegate: <StoryViewController: 0xfb1a30>

Edit:UIWebView subclass (MyWebView) which is subview of MyScrollView works fine and (void)webViewDidFinishLoad:(UIWebView*)webView (in the same storyviewcontroller.m) prints message.

No errors, builds fine, runs fine. After scrolling - nothing gets printed.

What am I doing wrong?

+2  A: 

Are you sure that scrollView is in fact the scroll view that is displayed? A couple of things I can think of that I would check in order of desperation:

  1. Check that I actually added scrollView to self.view after self.view was loaded
  2. Check that when I add scrollView to self.view, it's the same object I had when I was assigning the delegate
  3. Checkwhen I add scrollView that scrollView's delegate is still set to the view controller
  4. Check that self is, in fact set to the view controller when assigning the delegate
  5. Try using a regular UIScrollView and see if anything changes
Ed Marty
Thanks, your info was useful, I've tried everything and much more, but still no luck. Even if I delegate UIScrollView to MyScrollView - nothing happens too. Maybe there is something wrong with delegated functions? Can they be unreachable?
sniurkst
Just to be clear, have you tried replacing the MyScrollView with a UIScrollView?There's no reason based on the code you have above that the delegate should not work, so it's something somewhere else that's broken.
Ed Marty
Yes, I've tried original UIScrollView - didn't help. Also it looks like scrollView.contentSize and .contentOffset holds zeros. Gosh, what have I done here..
sniurkst
Is it possible that UIWebView which is subview of MyScrollView is somehow intercepting and messing everything up? How could I test that?
sniurkst
I'm sorry, how is UIWebView a subview of your own class? UIWebView isn't even a subclass of UIScrollView, its more like a sibling class, both having UIScroller objects that internally manage the scrolling.
Ed Marty
+3  A: 

This may be a red herring, but you're calling alloc and init, rather than alloc and initWithFrame:. I assume you're adding the scrollView in your viewDidLoad method? Is this a XIB-based controller? Have you tried adding the scrollview in the XIB file, and setting things up there? Have you tried adding other delegate methods?

Ben Gottlieb
Should I use initWithFrame instead of just init? I really don't know this and I thought that it is normal to use "init" and set frame later. MyScrollView doesn't have view controller and so isn't XIB baised (i'm very awful with IB, so I try to do everything I can programatically). Other delegate methods doesn't work too. And my head hurts like hell, it supposed to be such an easy task...
sniurkst
Is it possible that UIWebView which is subview of MyScrollView is somehow intercepting and messing everything up? How could I test that?
sniurkst
initWithFrame: is the designated initializer for views; you should call it from your own init, and if you haven't overridden init, call it directly.It's very possible that if your UIWebView is a child of the UIScrollView, it's grabbing your events. The simplest thing to try is replace the UIWebView with a fixed UIImage in a UIImageView. UIWebView handles its own scrolling; you're going to have a very hard time interacting with it in this manner.
Ben Gottlieb
A: 

So, once again, stupidity award goes to sniurkst. It was in fact UIWebView which was incorectly added to UIScrollView, so to me it was "oh my god, my scroll view is working", while it still was UIWebView scrolling on it's own.

Sorry, for my stupidity. Thank you all for trying to help me, next time I'll add more code samples, so you can see what's going on and laugh at my mistakes :)

And if someone would point me to an example how to correctly use ScrollView with WebView inside, so that ScrollView would be responsible for all the scrolling, I would be very thankful.

sniurkst
sorry, there's no way to do this. WebViews handle their own scrolling. You might be able to use a fixed-size UIWebView and disable its userInteractionEnabled flag. Of course, then regular touches on it won't work. I'd recommend a different approach.
Ben Gottlieb
well, that's sad, because I need WebViews rich text with css/js and links and also I need to know what its scroll offset and when it is scrolled...thank you for your responses, maybe i'll figure something out.
sniurkst
A: 

Hi,

I have a rather complicated View Heirarchy.

The View Controller has on it two views : 'Menu' and 'Objects'. Both are UIView(s)

The Objects view has on it several different views, each of which are of a class 'BigList' which is itself derived from UIView.

Within each big list, I have two views: a UILabel, and a UIScrollView.

Within each UIScrollView, im adding UIViews and UIWebviews as and when required by the user.

All views are clipped to bounds.

I'm having a problem with the scroll. For the top 80% of the screen, the scrolling works just fine. But around the bottom 20% of the screen, the ViewController seems to be responding to the users touches. I cant figure out why this is. Can anyone please help out?

Aman Rai