xcode UIScrollViewDelegate 不触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6842568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:34:37  来源:igfitidea点击:

UIScrollViewDelegate not firing

iphoneobjective-cxcodecocoa-touch

提问by Eric Brotto

This may be some sort of duplicate of this question. But I've tried to apply it's answers to no avail.

这可能是这个问题的某种重复。但我试图应用它的答案无济于事。

I have a UIScrollViewwith pagingEnabled. I would like to use UIScrollViewDelegateto help detect which page I'm on. Here is my code:

我有一个UIScrollViewpagingEnabled。我想用它UIScrollViewDelegate来帮助检测我在哪个页面上。这是我的代码:

@interface SearchMissionViewController : UIViewController <UIScrollViewDelegate> {

    UIScrollView *scroll;

}
    @property (nonatomic, retain) UIScrollView *scroll;

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView;
    - (IBAction) popView;


    @end




- (void)viewDidLoad
{
    [super viewDidLoad] ;

    // Here I have also tried scroll.delegate = self;
    self.scroll.delegate = self;


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

    // Is this correct? I've added it to try to get the UIScrollView Delegate calling scrollViewDidEndScrollingAnimation (or whatever)
    // [scroll setContentOffset:CGPointMake(0.0, 0.0) animated:YES];


    scroll.pagingEnabled = YES;
    NSInteger numberOfViews = 3;
    for (int i = 0; i < numberOfViews; i++) {
        CGFloat yOrigin = i * self.view.frame.size.width;
        UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
        awesomeView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];


        UIImageView *targetView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"targetImageA.png"]];
        targetView.frame = CGRectMake(0.0, 0.0, 139, 153);
        targetView.frame = CGRectMake(yOrigin+90 , 80, 139, 153);


        UILabel *targetNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(yOrigin+95, 230, 100, 20)];
        targetNameLabel.text = @"Bond";
        targetNameLabel.backgroundColor = [UIColor clearColor];
        targetNameLabel.textColor = [UIColor redColor];


        UILabel *missionNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(yOrigin+95, 250, 100, 20)];
        missionNameLabel.text = @"Mission";
        missionNameLabel.backgroundColor = [UIColor clearColor];
        missionNameLabel.textColor = [UIColor redColor];



        [scroll addSubview:awesomeView];        
        [scroll addSubview:targetView];
        [scroll addSubview:targetNameLabel]; 
        [scroll addSubview:missionNameLabel];

        // Why does this get released? Wouldn't it then dissappear from the scroll view? Should I not be releasing other objects from above?
        [awesomeView release];


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




    // Overlap image
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    imgView.image= [UIImage imageNamed:@"mission-viewfinder.png"];
    [self.view addSubview:imgView];


    // Place back button ontop of the image. 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom ];
    [button addTarget:self 
               action:@selector(popView)
    forControlEvents:UIControlEventTouchDown];
    button.frame = CGRectMake(0.0, 0.0, 100.0, 60.0);
    [self.view addSubview:button];


    // Do any additional setup after loading the view from its nib.
}








- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@" ");
    NSLog(@" Scroll View Did End Decelerating");
    NSLog(@" ");  

}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@" ");
    NSLog(@" Scroll View Did End Scroll ! ! ! ");
    NSLog(@" ");  

}

Essentially the problem is that the two methods above, scrollViewDidEndDecleratingand scrollViewDidScrollare not printing. In other words they are not getting called.

本质问题是上面说的两种方法,scrollViewDidEndDecleratingscrollViewDidScroll不是打印。换句话说,他们没有被召唤。

Any ideas?

有任何想法吗?

回答by albertamg

The problem lies in the order of the statements:

问题在于语句的顺序:

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

You attempt to set the delegate before you create the scrollview so the statement has no effect because scrollis nil. Do this instead:

您尝试创建滚动视图之前,所以语句没有影响,因为设置委托scrollnil。改为这样做:

self.scroll = [[[UIScrollView alloc] initWithFrame:CGRectMake(...)] autorelease];
self.scroll.delegate = self;


I just noticed your comment:

我刚注意到你的评论:

// Why does this get released? Wouldn't it then dissappear from the scroll view? Should I not be releasing other objects from above?
[awesomeView release];

Yes, you should be releasing targetView, targetNameLabeland missionNameLabelafter you add them to the scrollView. You own them because you created them (via alloc-init) and you must relinquish ownership of them by sending a releasemessage to them. When you add them to the scrollView, it will retain them as stated in the documentation of addSubview:to make sure they don't get deallocated while the scrollView needs them.

是的,您应该释放targetViewtargetNameLabel然后missionNameLabel将它们添加到滚动视图。您拥有它们是因为您创建了它们(通过 alloc-init)并且您必须通过向它们发送release消息来放弃它们的所有权。当您将它们添加到 scrollView 时,它将按照文档中的说明保留它们,addSubview:以确保它们在 scrollView 需要它们时不会被释放。

回答by Robin

Instead of

代替

- (void)viewDidLoad
{
    // Here I have also tried scroll.delegate = self;
    self.scroll.delegate = self;
    self.view.backgroundColor = [UIColor redColor];
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
}

You should do this

你应该做这个

- (void)viewDidLoad
{
    self.view.backgroundColor = [UIColor redColor];
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    self.scroll.delegate = self;
}