I’m new to iPhone development and am working on this tutorial: http://www.iosdevnotes.com/2011/03/uiscrollview-paging/#comment-25166 – but am doing it in XCode 4.3.2. The example is pretty simple and I understand the code, but for some reason the scrollViewDidScroll function isn’t firing for me in my ViewController.m (this thus doesn’t change the Page Control pagination icons and update it to the current page). I placed an NSLog() in this function so I can tell if it is ever activating – but in my debug console I never see this text
The code from my ViewController.h is:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIScrollViewDelegate> {
UIScrollView* scrollView;
UIPageControl* pageControl;
}
@property (nonatomic, retain) IBOutlet UIScrollView* scrollView;
@property (nonatomic, retain) IBOutlet UIPageControl* pageControl;
@end
The code from my ViewController.m is:
#import "ViewController.h"
@implementation ViewController
@synthesize scrollView, pageControl;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subview];
[subview release];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = colors.count;
}
//!!!!!!!!!!!!!!
//THIS IS WHERE MY PROBLEM IS -- THIS CODE DOESN'T SEEM TO EVER GET RUN EVEN WHEN I SCROLL!!!!!
//!!!!!!!!!!!!!
- (void)scrollViewDidScroll:(UIScrollView *)sender {
NSLog(@"this just fired");
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
self.scrollView = nil;
self.pageControl = nil;
}
- (void)dealloc {
[scrollView release];
[pageControl release];
[super dealloc];
}
@end
Any help would be greatly appreciated. I’ve been going back and forth from the downloadable file they have on the tutorial to my own over and over and over… I just feel like this thing should fire when I start scrolling and have no idea why it’s not… Thanks in advance!
In Interface Builder you did not connect the scroll view’s “delegate” outlet to you view controller. But you can do it programmatically.