Search code examples
ios5uiscrollviewuipagecontroluiscrollviewdelegate

iOS iPad App: Delegates not called for ViewController with two UIScrollViews and a UIPageControl (delegate functions for paging NOT CALLED)


For my iPad App, I have a main ViewController which contains two UIScrollviews and a UIPageControl. The Problem is that the delegates for the paging are not getting called. Here is the layout:

enter image description here

Selecting a button in the lower thumbScrollView needs to update the image in the mainScrollView (this works) Swiping the thumbScrollView or picking a dot on the pageControl needs to "page" the thumbScrollView to show the next previous set of buttons. The swiping does not work because the delegate functions are just not getting called.

I declare the scrollviews and pagecontrol as follows in my VC

@property (strong, nonatomic) IBOutlet UIScrollView *mainScrollView;
@property (strong, nonatomic) IBOutlet UIScrollView *thumbScrollView;
@property (strong, nonatomic) IBOutlet UIPageControl *pageControl;

The ViewController implements UIScrollViewDelegate

@interface MyViewController : UIViewController<UIScrollViewDelegate>

And I implement the following UIScrollViewDelegate delegate functions in my VC's .m file.

- (void)scrollViewDidScroll:(UIScrollView *)sender;
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;

The view appears but when I swipe across the buttons I do not see the delegate functions above getting called.

I have not found a solution to this in StackOverflow although I have factored in advise from other related posts for other aspects of this (ex. the logic to distinguish which scrollview has initiated the action etc)

ADDING DETAILED CODE HERE (as requested by @HeWas)

This is the header file for the Main View Controller that controls the two scrollviews and pagecontrol (RELEVANT EXCERPTS - TELL ME IF YOU NEED MORE)

// ImageBrowseViewController.h
// (NOTE - In Interface Builder I have added a tag attribute of 0 to mainScrollView
//  and 1 to thumbScrollView, to enable me to distinguish which scrollView the delegate 
//  needs to respond to)



#define TAG_MAIN_SCROLLVIEW 0
#define TAG_THUMB_SCROLLVIEW 1

@interface ImageBrowseViewController : UIViewController<UIScrollViewDelegate>
{
    UIButton* currentlySelectedButton;
    UIScrollView *mainScrollView;
    UIScrollView *thumbScrollView;
    UIPageControl* pageControl;
    BOOL pageControlBeingUsed;

}

@property (strong, nonatomic) IBOutlet UIScrollView *mainScrollView;
    // … connected as outlet in IB to mainScrollView
@property (strong, nonatomic) IBOutlet UIScrollView * thumbScrollView;
    // … connected as outlet in IB to thumbScrollView
@property (strong, nonatomic) IBOutlet UIPageControl *pageControl;
    // … connected as outlet in IB to pageControl


…
-(IBAction)changePage; //Touch up Inside IBAction connected to pageControl


…
@end

This is the implementation file for the Main View Controller that controls the two scrollviews and pagecontrol (RELEVANT EXCERPTS - TELL ME IF YOU NEED MORE)

//
//  ImageBrowseViewController.m
//

    …
@synthesize mainScrollView;
@synthesize thumbScrollView;
@synthesize pageControl;


// UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)sender {

    if ( [sender tag] == TAG_THUMB_SCROLLVIEW ) {
        // This is the thumbScrollview
        // Update the page when more than 50% of the previous/next page is visible

        CGFloat pageWidth = self.thumbScrollView.frame.size.width;
        int page = 
            floor((self.thumbScrollView.contentOffset.x - pageWidth / 2) / pageWidth) 
            + 1;
        self.pageControl.currentPage = page;

    }
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    pageControlBeingUsed = NO;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    pageControlBeingUsed = NO;
}

- (IBAction)changePage {
    // Update the scroll view to the appropriate page
    CGRect frame;
    //frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
    frame.origin.x = self.thumbScrollView.frame.size.width * self.pageControl.currentPage;

    frame.origin.y = 0;
    frame.size = self.thumbScrollView.frame.size;
    [self.thumbScrollView scrollRectToVisible:frame animated:YES];

    // Keep track of when scrolls happen in response to the page control
    // value changing. If we don't do this, a noticeable "flashing" occurs
    // as the the scroll delegate will temporarily switch back the page
    // number.
    pageControlBeingUsed = YES;
}

Solution

  • You code all looks 100% (aside from this typo: @synthesize floorplanThumbScrollView;, but that isn't your problem).

    I am sure that the answer is that you have not correctly wired your scrollview DELEGATES in IB.

    This is the clue:

    "Yes I have set all three in Interface Builder. So mainScrollView, thumbScrollView, and pageControl are wired in IB to the above declarations in the VC's .h file."

    You need 2 connections between your ViewController and your scrollViews.

    (1) ctrl-drag FROM viewController TO scrollView, connect to IBOutlet property.
    This is what you have done.

    (2) ctrl-drag FROM scrollView TO viewController, connect to delegate.
    I do not think you have done this.

    Explanation of step 2

    UIScrollView has a built-in property called 'delegate'. The scrollView uses this property to send messages to it's delegate. You set this delegate in interface builder (step 2) or you can do it in code. For example in your viewController you could do this:

        [myScrollView setDelegate:self];
    

    which would set the viewController as the delegate for myScrollView. If you do it by linking in Interface Builder you don't need this code (and IB doesn't create any).

    Either way what this actually does is set scrollView's delegate iVar to a pointer to the viewController. The great thing about using delegates like this is that the delegator (UIScrollView) doesn't have to know anything about the delegatee (in this case your UIViewController). This allows us to reuse UIScrollView so long as we observe it's delegate protocol.

    Whenever the scrollView needs to notify it's delegate, internally it sends a message like this..

        [self.delegate scrollViewDidScroll:self];
    

    (you don't see that, it's in the scrollView's implementation).

    The object that you have set as the delegate to scrollView needs to implement all of the required methods that the scrollView's delegate protocol declares, and can choose to implement any of the optional delegate methods. Here is the protocol

    To work out which methods are required, read the UIScrollView class reference, which tells you this:

    The UIScrollView class can have a delegate that must adopt the UIScrollViewDelegate protocol. For zooming and panning to work, the delegate must implement both viewForZoomingInScrollView: and scrollViewDidEndZooming:withView:atScale:; in addition, the maximum (maximumZoomScale) and minimum (minimumZoomScale) zoom scale must be different.

    Everything else in the protocol is optional.

    This delegate pattern is one you can easily implement yourself for your own object reuse, and is one of the most common ways of passing messages between decoupled objects in objective-C.