0

我正在从一本书中学习 iOS 编程,并且我编写了一个应用程序来使页面转到另一页,类似于 iBooks。但是当我在模拟器中运行它时,当我向左或向右滑动时,页面根本不会转动或转换。我不知道为什么。

我认为相关的代码如下。我还附上了整个项目,以防我错过任何东西。基本上有一个 UIWebview 是 UIViewController 的内容。它从充当 PageView 数据源的类中的模型填充。根据您转到适当(协议)方法的视图控制器,然后根据您要使用的视图控制器的索引设置 webview 中的内容。

但是,它不会起作用,正如所说:

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    NSUInteger index = [self indexOfViewController:(ContentViewController *)viewController];

    if (index == 0 || index == NSNotFound) {
        return nil;
    }
    else {
        index--;
        return [self viewControllerAtIndex:index];
    }
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSUInteger index = [self indexOfViewController:(ContentViewController *)viewController];

    if (index == NSNotFound) {
        return nil;
    }
    else {
        index--;
        return [self viewControllerAtIndex:index];
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self createContentPages];

    NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin]
        forKey:UIPageViewControllerOptionSpineLocationKey];

    _pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
        navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
        options: options];

    _pageController.dataSource = self;
    [[_pageController view] setFrame:[[self view] bounds]];

    ContentViewController *initialViewController = [self viewControllerAtIndex:0];

    NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];

    [_pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward
        animated:NO
        completion:nil];

    [self addChildViewController:_pageController];

    [[self view] addSubview:[_pageController view]];

    [_pageController didMoveToParentViewController:self];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createContentPages {
    NSMutableArray *pageStrings = [[NSMutableArray alloc] init];

    for (int i = 1; i <= 10; i++) {
        NSString *contentString = [[NSString alloc] initWithFormat:@"<html><head></head><body><h1>Chapter %d</h1><p>This is the page %d of content displayed using UIPageViewController in iOS 6.</p></body></html>", i, i];
        [pageStrings addObject:contentString];
    }

    self.pageContent = [[NSArray alloc] initWithArray:pageStrings];
}

// Return the data view controller for the given index
- (ContentViewController *)viewControllerAtIndex:(NSUInteger)index {
    if (([self.pageContent count] == 0) || index >= [self.pageContent count]) {
        return nil;
    }
    else {
        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];

        ContentViewController *dataViewController = [storyBoard instantiateViewControllerWithIdentifier:@"contentView"];

        dataViewController.dataObject = self.pageContent[index];

        return dataViewController;
    }
}

- (NSUInteger)indexOfViewController:(ContentViewController *)viewController {
    return [self.pageContent indexOfObject:viewController.dataObject];
}

整个项目:http ://cl.ly/1B0l3Z1H1616

4

1 回答 1

1

在里面

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController 

方法,你有

index--

尝试将其更改为

index++
于 2012-12-12T21:13:51.670 回答