UIScrollView和UIViews

时间:2011-04-11 01:03:56

标签: iphone sdk uiscrollview

当用户滚动浏览UIScrollView控件时,我想使用UIScrollView显示多个UIViews。我现在还不担心表现出分页。

我已经设法实现了一些,但是没有按照我想要的方式工作。

目前:

我有3个带有不同nib文件的ViewControllers。根视图控制器是带有UIScrollView的控制器,它可以加载其余的视图控制器。

NSMutableArray *controllers = [[NSMutableArray alloc] init];
    for (unsigned i = 0; i < kNumberOfPages; i++) {
        [controllers addObject:[NSNull null]];
    }
    self.viewControllers = controllers;
    [controllers release];

    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;

    [self loadScrollViewWithPage:0];


- (void)loadScrollViewWithPage:(int)page {
    if (page == 0) {
        PageTwo *controller2 = [viewControllers objectAtIndex:page];
        if ((NSNull *)controller2 == [NSNull null]) {
            controller2 = [[PageTwo alloc] initWithPageNumber:page];
            [viewControllers replaceObjectAtIndex:page withObject:controller2];
            [controller2 release];
        }
        // add the controller's view to the scroll view
        if (nil == controller2.view.superview) {
            CGRect frame = scrollView.frame;
            frame.origin.x = frame.size.width * page;
            frame.origin.y = 0;
            controller2.view.frame = frame;
            [scrollView addSubview:controller2.view];
        }
    }
    if (page == 1) {

    // replace the placeholder if necessary
    NewController *controller = [viewControllers objectAtIndex:page];
    if ((NSNull *)controller == [NSNull null]) {
        controller = [[NewController 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];
    }
    }

这一切都很好......但是,我想要它做的是首先加载根笔尖。但我不知道该如何解决这个问题。我知道我必须将页面数量增加到3,但是当我初始化控制器时,我不知道如何告诉它页面== 0应该是当前视图。

有什么想法吗?

更新

叹了口气,我忽略了一些东西..没有注意到如果不在0级指定页面,它只显示当前的视图哈哈!

傻傻的我。

1 个答案:

答案 0 :(得分:0)

我忽略了一些东西..没有注意到如果不在0级指定页面,它只显示当前视图哈哈!