我是Obj-C的新手,也是学习它的舞台。刚刚看到那些代码并试图得到它。但是,在第一个以 - (UIViewController *)开头的代码中,pageViewController ....对我来说太复杂了。
任何可以一步一步解释的人?
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [self.pageTitles count]) {
return nil;
}
return [self viewControllerAtIndex:index];
}
答案 0 :(得分:1)
基本上你有一个UIPageViewController。它目前在所有N页中显示索引K处的页面。 UIPageViewController需要知道在显示的当前页面之前(在索引K处)和当前页面之后的页面之前出现了什么页面。
以下是它的外观:
//Called on the data source. Returns the UIViewController before the
//current one (viewController) within the UIPageViewController (pageViewController)
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
//gets the index of the page currently showing
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;
//Checks that it's not the first page and that it's
//actually a part of the UIPageViewController
if ((index == 0) || (index == NSNotFound)) {
//If it is, then there is no page before it.
return nil;
}
//otherwise, lets go back one index
index--;
//and then this is the UIViewController before it
return [self viewControllerAtIndex:index];
}
对于第二种方法,它正在做同样的事情,但是试图获取后面的页面。同样,iOS需要了解这些内容,因此,在高级别,它可以在用户更改页面时快速显示该页面。
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
//gets the index of the current page being shown
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;
//if it's not found, then there isn't a page after it
if (index == NSNotFound) {
return nil;
}
//go to the next index
index++;
//if we're at the end of the pages
if (index == [self.pageTitles count]) {
//there's no page after it
return nil;
}
//otherwise, return the next UIViewController
return [self viewControllerAtIndex:index];
}