2011-07-27 24 views
4

これは一部の場合はsort of duplicate of this questionです。しかし、私はそれを役に立たない答えを適用しようとしました。UIScrollViewDelegateが起動しない

UIScrollViewpagingEnabledです。 UIScrollViewDelegateを使用して、現在表示されているページを検出するのに役立ちます。基本的に問題は二つの方法は、上記の、scrollViewDidEndDecleratingscrollViewDidScrollが印刷されないということです

@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(@" "); 

} 

:ここに私のコードです。言い換えれば、彼らは呼ばれていません。

アイデア?

答えて

15

問題は文の順序である:scrollnilであるため、ステートメントは効果がありませんので、あなたがscrollviewを作成する前に、デリゲートを設定しようと

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

。代わりに、次の操作を行います。

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

は、私はちょうどあなたのコメントに気づい:

// 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]; 

はい、あなたはscrollViewに追加した後targetViewtargetNameLabelmissionNameLabelを解放する必要があります。あなたはそれらを所有しています(alloc-init経由で)ので、releaseメッセージを送信して所有権を放棄しなければなりません。 scrollViewに追加すると、addSubview:のドキュメントに記載されているようにそれらが保持され、scrollViewがそれらを必要としている間に割り当てが解除されないことを確認します。

1

代わり

- (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)]; 
} 

のあなたは、この

- (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; 
} 
を行う必要があります
関連する問題