iOS5 – How do I add a page array to UIPAGEVIEWCONTROLLER in iOS 5?

Does anyone know what I am missing in order to manually add my own view controller to the UIPageViewController method?

I currently have this, I don’t know how to proceed:

NSDictionary *pageViewOptions = [NSDictionary dictionaryWithObjectsAndKeys:UIPageViewControllerOptionSpineLocationKey, [NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin ], nil];

self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:pageViewOptions];


BookViewController *pageOne = [ self.storyboard instantiateViewControllerWithIdentifier:@"BookViewController"];

AnotherPage *pageTwo = [self.storyboard instantiateViewControllerWithIdentifier:@"AnotherPage"];

PageThree *pageThree = [self. storyboard instantiateViewControllerWithIdentifier:@"PageThree"];


self.pageViewController.delegate = self;
self.pageViewController.dataSource = self;

[ self.pageViewController setViewControllers:[NSArray arrayWithObje cts:pageOne, pageTwo, pageThree, nil] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

[self addChildViewController:self.pageViewController];

[self.view addSubview:self.pageViewController.view];

self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;

[self.pageViewController didMoveToParentViewController:self];

But it seems to be useless. I only see RootViewController.

Thanks to all those who like to help in advance who are eager for newbies like me….

The way you set it is not exactly the way UIPageViewController works.

The first thing to understand is setViewControllers:direction:animated : Completion only sets the visible view controller, not all the view controllers you want to show to the user. (That is, if I try to set an array of three controllers in the code, I will get an exception.)

Secondly, if you have multiple visible pages (or two, depending on the trunk location), you must implement the UIPageViewControllerDataSource protocol to provide the view controller that the page view controller will display.

This is a short example (implemented as a subclass of UIPageViewController, but also applicable to subpage view controllers… Just replace self.dataSource =… with myPageViewController.dataSource = …)

< /p>

@implementation MyPageViewControll er {
// we keep our page view controllers in an array, but we could just as easily
// instantiate them on the fly in the data source methods
NSArray *_pages;
}

- (void)setupPages {
/*
* set up three pages, each with a different background color
*/
UIViewController *a = [[UIViewController alloc] initWithNibName:nil bundle:nil];
a.view.backgroundColor = [UIColor redColor];
UIViewController *b = [[UIViewController alloc] initWithNibName:nil bundle:nil ];
b.view.backgroundColor = [UIColor greenColor];
UIViewController *c = [[UIViewController alloc] initWithNibName:nil bundle:nil];
c.view.backgroundColor = [UIColor blueColor];
_pages = @[a, b, c];
}

- (void)viewDidLoad {
[super viewDidLoad];
[self setupPages];
self.dataSource = self;
// set the initially visible page's view controller... if you don't do this
// you won't see anything.
[self setViewControllers:@[_pages[0]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {
}];< br />)

#pragma mark-UIPageViewControllerDataSource

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
if (nil == viewController) {
return _pages[0];
}
NSInteger idx = [_pages indexOfObject:viewController];
NSParameterAssert(idx != NSNotFound);
if (idx >= [_pages count]-1) {
// we're at the end of the _pages array
return nil;
}
// return the next page's view controller
return _pages[idx + 1];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewContro ller {
if (nil == viewController) {
return _pages[0];
}
NSInteger idx = [_pages indexOfObject:viewController];
NSParameterAssert(idx != NSNotFound);
if (idx <= 0) {
// we're at the end of the _pages array
return nil;
}
/ / return the previous page's view controller
return _pages[idx-1];
}

Does anyone know that I want to control my own view? Manually added to the UIPageViewController method and what is missing?

I currently have this, I don’t know how to proceed:

NSDictionary *pageViewOptions = [NSDictionary dictionaryWithObjectsAndKeys:UIPageViewControllerOptionSpineLocationKey, [NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin ], nil];

self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:pageViewOptions];


BookViewController *pageOne = [ self.storyboard instantiateViewControllerWithIdentifier:@"BookViewController"];

AnotherPage *pageTwo = [self.storyboard instantiateViewControllerWithIdentifier:@"AnotherPage"];

PageThree *pageThree = [self. storyboard instantiateViewControllerWithIdentifier:@"PageThree"];


self.pageViewController.delegate = self;
self.pageViewController.dataSource = self;

[ self.pageViewController setViewControllers:[NSArray arrayWithObjects :pageOne, pageTwo, pageThree, nil] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

[self addChildViewController:self.pageViewController];

[self.view addSubview :self.pageViewController.view];

self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;

[self.pageViewController didMoveToParentViewController:self];

< p>But it doesn’t seem to be useful. I only see RootViewController.

Thanks to all those who like to help in advance who are eager for newbies like me….

The way you set it is not exactly how UIPageViewController works.

The first thing to understand is setViewControllers:direction:animated:completion only sets the visible view controllers, not all you The view controller that I want to display to the user. (That is, if I try to set an array of three controllers in the code, I will get an exception.)

Secondly, if you have more than one visible Page (or two, depending on the trunk location), you must implement the UIPageViewControllerDataSource protocol to provide the view controller that the page view controller will display.

This is a short example (as a subclass of UIPageViewController) Implementation, but also applicable to child page view controllers… Just replace self.dataSource =… with myPageViewController.dataSource = …)

@implementation MyPageViewController {
// we keep our page view controllers in an arr ay, but we could just as easily
// instantiate them on the fly in the data source methods
NSArray *_pages;
}

- (void)setupPages {
/*
* set up three pages, each with a different background color
*/
UIViewController *a = [[UIViewController alloc] initWithNibName:nil bundle:nil];
a.view.backgroundColor = [UIColor redColor];
UIViewController *b = [[UIViewController alloc] initWithNibName:nil bundle:nil];
b.view.backgroundColor = [UIColor greenColor] ;
UIViewController *c = [[UIViewController alloc] initWithNibName:nil bundle:nil];
c.view.backgroundColor = [UIColor blueColor];
_pages = @[a, b, c ];
}

- (void)viewDidLoad {
[super viewDidLoad];
[self setupPages];
self.dataSource = self;< br /> // set the initially visible page's view controller... if you don't do this
// you won't see anything.
[self setViewC ontrollers:@[_pages[0]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {
}];
}

#pragma mark-UIPageViewControllerDataSource

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
if (nil == viewController) {
return _pages[0];
}
NSInteger idx = [_pages indexOfObject:viewController];
NSParameterAssert(idx != NSNotFound);
if (idx >= [_pages count]-1) {
// we're at the end of the _pages array
return nil;
}
// return the next page's view controller
return _pages[idx + 1];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
if (nil == viewController) {
re turn _pages[0];
}
NSInteger idx = [_pages indexOfObject:viewController];
NSParameterAssert(idx != NSNotFound);
if (idx <= 0) {< br /> // we're at the end of the _pages array
return nil;
}
// return the previous page's view controller
return _pages[idx-1] ;
}

Leave a Comment

Your email address will not be published.