gpt4 book ai didi

ios - 为什么我的 iOS 应用程序无法进行翻页?

转载 作者:行者123 更新时间:2023-11-29 04:15:03 26 4
gpt4 key购买 nike

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

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

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

- (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

最佳答案

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

方法,你有

index--

尝试将其更改为

index++

关于ios - 为什么我的 iOS 应用程序无法进行翻页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13848208/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com