页面滚动查看帮助,每个页面的不同视图控制器?

时间:2009-10-18 06:26:15

标签: iphone uiviewcontroller uiscrollview

好的,所以我正在尝试使用两个视图的pagescrollView,每个视图都有一个不同的视图控制器,这样我就可以处理每个不同的视图。我得到了苹果的示例代码,似乎他们创建了很多不同的视图“懒得像他们说的那样”如何改变这些代码,以便我可以让第一页成为一个视图控制器而第二页是另一个视图控制器? / p>

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    // view controllers are created lazily
    // in the meantime, load the array with placeholders which will be replaced on demand
    NSMutableArray *controllers = [[NSMutableArray alloc] init];
    for (unsigned i = 0; i < kNumberOfPages; i++) {
        [controllers addObject:[NSNull null]];
    }
    self.viewControllers = controllers;
    [controllers release];

    // a page is the width of the scroll view
    scrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.scrollsToTop = NO;
    scrollView.delegate = self;

    pageControl.numberOfPages = kNumberOfPages;
    pageControl.currentPage = 0;

    // pages are created on demand
    // load the visible page
    // load the page on either side to avoid flashes when the user starts scrolling
    [self loadScrollViewWithPage:0];
    [self loadScrollViewWithPage:1];
}

- (void)loadScrollViewWithPage:(int)page {
    if (page < 0) return;
    if (page >= kNumberOfPages) return;

    // replace the placeholder if necessary
    MyViewController *controller = [viewControllers objectAtIndex:page];
    if ((NSNull *)controller == [NSNull null]) {
        controller = [[MyViewController alloc] initWithPageNumber:page];
        [viewControllers replaceObjectAtIndex:page withObject:controller];
        [controller release];
    }

    // add the controller's view to the scroll view
    if (nil == controller.view.superview) {
        CGRect frame = scrollView.frame;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0;
        controller.view.frame = frame;
        [scrollView addSubview:controller.view];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    // We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
    // which a scroll event generated from the user hitting the page control triggers updates from
    // the delegate method. We use a boolean to disable the delegate logic when the page control is used.
    if (pageControlUsed) {
        // do nothing - the scroll was initiated from the page control, not the user dragging
        return;
    }
    // Switch the indicator when more than 50% of the previous/next page is visible
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    pageControl.currentPage = page;

    // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
    [self loadScrollViewWithPage:page - 1];
    [self loadScrollViewWithPage:page];
    [self loadScrollViewWithPage:page + 1];

    // A possible optimization would be to unload the views+controllers which are no longer visible
}

// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    pageControlUsed = NO;
}

- (IBAction)changePage:(id)sender {
    int page = pageControl.currentPage;
    // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
    [self loadScrollViewWithPage:page - 1];
    [self loadScrollViewWithPage:page];
    [self loadScrollViewWithPage:page + 1];
    // update the scroll view to the appropriate page
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    [scrollView scrollRectToVisible:frame animated:YES];
    // Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
    pageControlUsed = YES;
}

谢谢,如果有人知道如何做到这一点。

2 个答案:

答案 0 :(得分:6)

要加载具有不同视图控制器的页面,您只需要更改这段代码

MyViewController *controller = [viewControllers objectAtIndex:page]; 
if ((NSNull *)controller == [NSNull null]) 
{ controller = [[MyViewController alloc] initWithPageNumber:page]; 
[viewControllers replaceObjectAtIndex:page withObject:controller];
 [controller release]; 
}

将每个视图控制器初始化为您想要的视图控制器而不是每次都相同,所以代码看起来像

 if(page==0)
{
 MyViewControllerZero *controller = [viewControllers objectAtIndex:page]; 
    if ((NSNull *)controller == [NSNull null]) 
    { controller = [[MyViewControllerZero alloc] initWithPageNumber:page]; 
    [viewControllers replaceObjectAtIndex:page withObject:controller];
     [controller release]; 
    }
}
  if(page==1)
{
 MyViewControllerOne *controller = [viewControllers objectAtIndex:page]; 
    if ((NSNull *)controller == [NSNull null]) 
    { controller = [[MyViewControllerOne alloc] initWithPageNumber:page]; 
    [viewControllers replaceObjectAtIndex:page withObject:controller];
     [controller release]; 
    }
}
//and so on ...

答案 1 :(得分:1)

这是正确的代码!!!

- (void)loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= kNumberOfPages) return;

// replace the placeholder if necessary
if (page == 0){
MyViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
    controller = [[MyViewController alloc] initWithPageNumber:page];
    [viewControllers replaceObjectAtIndex:page withObject:controller];

}
    if (nil == controller.view.superview) {
        CGRect frame = scrollView.frame;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0;
        controller.view.frame = frame;
        [scrollView addSubview:controller.view];
}
}
if (page == 1){
    PageOne *controller = [viewControllers objectAtIndex:page];
    if ((NSNull *)controller == [NSNull null]) {
        controller = [[PageOne alloc] initWithPageNumber:page];
        [viewControllers replaceObjectAtIndex:page withObject:controller];

    }
    if (nil == controller.view.superview) {
        CGRect frame = scrollView.frame;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0;
        controller.view.frame = frame;
        [scrollView addSubview:controller.view];
}
// add the controller's view to the scroll view

}

}

THX对于第一个问题,她开口了我的心!