1

我有一个 VC,其中实现了滚动视图和页面控件。它有三个页面,它们都设置正确并且工作正常。我想要做的是根据用户按下的按钮将 VC 加载到不同的页面上。

在按钮调用的方法中我有

ViewController *vc = [[ViewController alloc] init];
vc.pageControl.currentPage = 2;
[self.navigationController pushViewController:vc animated:YES]; 

当 VC 加载时,它被加载到正确的页面(如页面底部的气泡所示),但它正在显示来自页面 [0] 的信息。如果我开始滚动,无论我做什么,它都会跳回 page[0]。

我最好的猜测是,我需要根据我要显示的页面将 VC 加载到特定的 x 原点,但我无法找到与此相关的任何内容,所以我很迷茫。

感谢任何帮助。

编辑:这是来自 VC 类的代码。这不是我写的,这是我第一次使用 PageControl,所以我正在尝试边做边学。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {

    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.pagingEnabled = TRUE;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;

    firstPage = [[UIView alloc] init];
    firstPage.tag = 0;

    secondPage = [[UIView alloc] init];
    secondPage.tag = 1;

    thirdPage = [[UIView alloc] init];
    thirdPage.tag = 2;

    pages = [[NSArray alloc] initWithObjects:firstPage, secondPage, thirdPage, nil];

    // Page Control
    self.pageControl = [[UIPageControl alloc] init];
    self.pageControl.numberOfPages = 3;
    self.pageControl.currentPage = 0;

    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.pageControl];
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

// First Page Setup
//
[self setupFirstPage];

// Second Page Setup
//
[self setupSecondPage];

// Third Page Setup
//
[self setupThirdPage];

// Frame everything
//
CGRect screenFrame = [[UIScreen mainScreen] applicationFrame];
CGRect navBarFrame = self.navigationController.navigationBar.frame;

self.scrollView.frame = CGRectMake(0, navBarFrame.size.height, 320, screenFrame.size.height - navBarFrame.size.height);
self.pageControl.frame = CGRectMake(0, screenFrame.size.height - 70, 320, 20);

firstPage.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
secondPage.frame = CGRectMake(320 * secondPage.tag, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
thirdPage.frame = CGRectMake(320 * thirdPage.tag, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);

// Add Subviews
//
[self.scrollView addSubview:firstPage];
[self.scrollView addSubview:secondPage];
[self.scrollView addSubview:thirdPage];

self.scrollView.contentSize = CGSizeMake(320 * pages.count, self.scrollView.frame.size.height);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
4

1 回答 1

2

尝试添加此函数并在设置页码后立即调用它:

- (void)pageSelectorChanged 
{
    CGRect frame = self.scrollView.frame;
    frame.origin.x = frame.size.width * self.pageControl.currentPage;
    frame.origin.y = 0;
    [self.scrollView scrollRectToVisible:frame animated:NO];
}
于 2012-07-24T20:34:00.007 回答