Objective-c – UIScrollView on a UIViewController

objective c

I am working on a menu of an application. The problem now is that i can't get the UIScrollView working. The UIScrollView is in a Xib file.
It loads normal but when I try to scroll it crashed.

I think the problem is that I use a UIViewController since when I remove the delegate not to itself it is working. Now I got: scrollView.delegate = self;

When the delegate is the appDelegate it is working but that is not what I want.

The following functions I used in my class:

  • (void)loadScrollViewWithPage:(int)page
  • (void)scrollViewDidScroll:(UIScrollView *)sender
  • (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

I think I got 2 options. Create a NSObject with a view (is this possible) or a object that is the delegate of the UIScrollview.

The error what i gaves is:

2009-11-14 17:50:25.723 Par23[1240:20b] * -[NSCFType scrollViewDidScroll:]: unrecognized selector sent to instance 0x4834190
2009-11-14 17:50:25.724 Par23[1240:20b] *
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFType scrollViewDidScroll:]: unrecognized selector sent to instance 0x4834190'

It is not a problem of the function. When I remove all the code in the functions it still gave this error

The scrollview got the following settings:
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;

Best Answer

I had the same problem with UIScrollViewDelegte. I was setting the view controller that created my UIScrollView to be the delegate for that UIScrollView and I was also getting the unrecognized selector sent to instance problem that you list above.

In my view controller this code crashes.

    // create the scrollview
    scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];

    // add content view to the scrollView
    scrollView.contentSize = contentFrame.size;
    scrollView.scrollEnabled = YES;
    scrollView.clipsToBounds = YES;
    scrollView.delegate = self;
    [scrollView addSubview:contentView];

I worked around this by subclassing UIScrollView and making that the delegate. So now my scrollView is its own delegate. Here is the changed code.

    // create the scrollview
    scrollView = [[PMStickyHeaderScrollView alloc] initWithFrame:scrollFrame];

    // add content view to the scrollView
    scrollView.contentSize = contentFrame.size;
    scrollView.scrollEnabled = YES;
    scrollView.clipsToBounds = YES;
    scrollView.delegate = scrollView;  // notice that the scrollView is its own delegate
    [scrollView addSubview:contentView];

This workaround is sufficient for me. Here is my simple implementation of my scrollView subclass.

@interface PMStickyHeaderScrollView : UIScrollView <UIScrollViewDelegate> 
@end

@implementation PMStickyHeaderScrollView

- (void)scrollViewDidScroll:(UIScrollView *)sView
{
    NSLog(@"scrollViewDidScroll %@", [sView description]);
}

@end

For what it's worth, I believe that the root cause of the problem is incorrect handling of the responder chain by the UIScrollView when the UIScrollView's delegate is set, but I haven't seen any bugs reported about this. I'm using iPhone 3.0 SDK. What is everyone else using? Any other smart workarounds for this problem?

Related Topic