我是编程新手,正在尝试使用 xib 制作 PageViewController。
我遵循了在 SO 上找到的教程,该教程向我展示了如何在情节提要中使用不同的 UIViewController 制作 PageViewController(我已粘贴下面的代码)。但是,这很好用,对于我的最终应用程序,我想在 xib 中使用 PageViewController(具有多个不同的 xib)。
#import "IntroPages.h"
@implementation IntroPages
{
NSArray *myViewControllers;
}
-(void)viewDidLoad
{
[super viewDidLoad];
self.delegate = self;
self.dataSource = self;
UIViewController *p1 = [self.storyboard instantiateViewControllerWithIdentifier:@"1ID"];
UIViewController *p2 = [self.storyboard instantiateViewControllerWithIdentifier:@"2ID"];
UIViewController *p3 = [self.storyboard instantiateViewControllerWithIdentifier:@"3ID"];
UIViewController *p4 = [self.storyboard instantiateViewControllerWithIdentifier:@"4ID"];
myViewControllers = @[p1,p2,p3,p4];
[self setViewControllers:@[p1]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO completion:nil];
}
-(UIViewController *)viewControllerAtIndex:(NSUInteger)index
{
return myViewControllers[index];
}
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger currentIndex = [myViewControllers indexOfObject:viewController];
--currentIndex;
currentIndex = currentIndex % (myViewControllers.count);
return [myViewControllers objectAtIndex:currentIndex];
}
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
NSUInteger currentIndex = [myViewControllers indexOfObject:viewController];
++currentIndex;
currentIndex = currentIndex % (myViewControllers.count);
return [myViewControllers objectAtIndex:currentIndex];
}
-(NSInteger)presentationCountForPageViewController: (UIPageViewController *)pageViewController {
return myViewControllers.count;
}
-(NSInteger)presentationIndexForPageViewController: (UIPageViewController *)pageViewController {
return 0;
}