首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIScrollViewDelegate未触发

UIScrollViewDelegate未触发
EN

Stack Overflow用户
提问于 2011-07-27 18:03:01
回答 2查看 8.7K关注 0票数 4

这可能是一些sort of duplicate of this question。但我试着应用它的答案,但没有用。

我和pagingEnabled有个UIScrollView。我想使用UIScrollViewDelegate来帮助检测我在哪个页面上。下面是我的代码:

代码语言:javascript
复制
@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(@" ");  

}

本质上,问题在于上面的两个方法scrollViewDidEndDecleratingscrollViewDidScroll没有打印出来。换句话说,他们不会被调用。

有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-27 18:10:35

问题出在语句的顺序上:

代码语言:javascript
复制
self.scroll.delegate = self;
...
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

您尝试在创建滚动视图之前设置委托,因此该语句无效,因为scrollnil。改为执行以下操作:

代码语言:javascript
复制
self.scroll = [[[UIScrollView alloc] initWithFrame:CGRectMake(...)] autorelease];
self.scroll.delegate = self;

我刚注意到你的评论:

代码语言:javascript
复制
// 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];

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

票数 15
EN

Stack Overflow用户

发布于 2011-07-27 18:14:39

而不是

代码语言:javascript
复制
- (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)];
}

你应该这样做

代码语言:javascript
复制
- (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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6842568

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档