为什么我的页面无法在我的iOS应用程序中运行?

时间:2012-12-12 20:40:35

标签: ios xcode ios6

我正在从书中学习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

1 个答案:

答案 0 :(得分:1)

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

方法,你有

index--

尝试将其更改为

index++