2011-12-05 31 views
2

你好我試圖在用戶滾動應用程序時觸發一個函數。UIScrollViewDelegate不工作

這裏是我的代碼

我定義的類MyViewController它實現了UIScrollViewDelegate

@interface CircleViewController : UIViewController <UIScrollViewDelegate> { 
    UIScrollView *scroll; 
} 

@property (retain, nonatomic) UIScrollView * scroll; 

@end 

然後,在.M,在viewDidLoad中我寫道:

- (void)viewDidLoad { 
[super viewDidLoad]; 
self.scroll = [[UIScrollView alloc] init]; 
scroll.delegate = self; 
[scroll setBackgroundColor:[UIColor whiteColor]]; 

NSInteger nbView = 3; 


scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height)]; 

for(int i = 0; i < nbView; i++) { 
    CGFloat xOrigin = i * self.view.frame.size.height; 

    UIView * vue = [[UIView alloc] initWithFrame:CGRectMake(0, xOrigin,  self.view.frame.size.width, self.view.frame.size.height)]; 
    vue.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1]; 
    [scroll addSubview:vue]; 
    [vue release]; 

} 
scroll.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height * nbView); 
[scroll setScrollEnabled:YES]; 
[self.view addSubview:scroll]; 

我定義觸發:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView { 
    NSLog(@"test"); 
} 

當我運行應用程序時,我可以滾動,但scrollViewDidScroll永遠不會被調用。

回答

6

你兩次分配滾動視圖可能是什麼原因造成的問題... 你只需要一次的Alloc,然後設置委託。

+0

哇THX ANKIT,我沒看出來,這麼愚蠢:) – flocks

+0

滾動的第二分配覆蓋的第一個,並委託丟失。你可能想第二次使用[scroll setFrame:]來代替scroll = [[UIScrollView alloc] ...],因爲alloc會創建一個新的滾動視圖 – Chaosphere2112