Marko Heijnen
Marko Heijnen

Reputation: 56

UIScrollView on a UIViewController

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:

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;

Upvotes: 0

Views: 7567

Answers (3)

Tiago Martins
Tiago Martins

Reputation: 11

I came across that same problem. Accidentally fixed it when I stopped releasing the view controller (that had the scrollView) after adding it to a view. I'm not sure why this happens, because I'm sure that releasing the view controller after adding it to a view is the right thing to do, since I'm not doing anything else with it.

Upvotes: 1

JLads
JLads

Reputation: 21

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?

Upvotes: 2

marcc
marcc

Reputation: 12399

Setting the delegate of your ScrollView to the ViewController is certainly an acceptable design. You need to open the Console while running your app in the simulator to see what the error is. It's going to be next to impossible to diagnose without the error log.

Also, you might want to consider posting the code to your scroll view delegate functions.

Upvotes: 1

Related Questions