How to add an Array of pages to a UIPageViewController in iOS 5?

前端 未结 3 567
囚心锁ツ
囚心锁ツ 2021-01-14 14:46

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

I currently have this and I do not know how

3条回答
  •  不要未来只要你来
    2021-01-14 15:07

    The way you've set this up isn't quite how UIPageViewController works.

    The first thing to understand is that setViewControllers:direction:animated:completion only sets the visible view controllers, not all the view controllers you may wish to display to the user. (To wit, if I try to set an array of three controllers as in your code, I get an exception.)

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

    Here's a short example (implemented as a subclass of UIPageViewController, but would work with a child page view controller, too... just replace self.dataSource = ... with myPageViewController.dataSource = ...)

    @implementation MyPageViewController {
        // 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) {
        }];
    }
    
    #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) {
            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];
    }
    

提交回复
热议问题