UIScrollViewDelegate无法正常工作

时间:2011-12-05 21:48:56

标签: iphone uiscrollviewdelegate

您好我正在尝试在用户滚动应用时触发一个功能。

这是我的代码

我定义了一个实现UIScrollViewDelegate

的MyViewController类
@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永远不会被调用。

1 个答案:

答案 0 :(得分:6)

你分配两次滚动视图可能是导致问题的原因... 你只需要分配一次,然后设置委托。