2012-06-09 64 views
1

我是iPhone新手,正在研究本教程:http://www.iosdevnotes.com/2011/03/uiscrollview-paging/#comment-25166 - 但我在XCode 4.3.2中做了這個。這個例子非常簡單,我理解代碼,但由於某種原因,scrollViewDidScroll函數在我的ViewController.m中沒有爲我啓動(因此它不會更改Page Control分頁圖標並將其更新到當前頁面)。我把一個的NSLog()這個函數,所以我可以知道它是不斷激活 - 但在我調試控制檯我從來沒有看到這段文字iPhone Dev- Page Control - scrollViewDidScroll not firing

從我ViewController.h的代碼是:

#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 

的代碼從我的ViewController.m是:

#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 

任何幫助將不勝感激。我一直在從他們在教程中的可下載文件中來回回顧我自己的一遍又一遍......當我開始滾動時,我只是覺得這個東西應該觸發,不知道爲什麼它不是......提前致謝!

回答

7

在Interface Builder中,您沒有將滾動視圖的「委託」插座連接到您的視圖控制器。但是你可以通過編程來完成。

[self.scrollView setDelegate:self]; 
+0

非常感謝!這工作就像一個魅力......我不相信我花了這麼多小時試圖弄清楚這一點。我愛你,每個人都在stackoverflow! – Robbie

0

dianz is Right,Set your delegate。

[self.scrollView setDelegate:self]; 

OR

self.scrollView.delegate=self; 
0

你忘了把你的委託:

- (void)viewDidLoad { 
[super viewDidLoad]; 

// This is what you forgot to do, set the delegate of the scrollView. 
scrollView.delegate = self; 

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]; 
} 
0

請確保您已經滾動視圖的委託設置爲你想要的視圖 - 控制。 如果滾動視圖添加到xib中,您也可以將代理附加到xib文件中。

另外,別忘了在viewcontroller的頭文件中實現scrollview委託協議UIScrollViewDelegate。