Using UIPageViewController with swift and multiple view controllers

后端 未结 3 1018
心在旅途
心在旅途 2020-12-02 14:09

I\'m trying to utilize a UIPageViewController in a Swift app with multiple view controllers. I have 2 view controllers on my storyboard (firstViewControll

相关标签:
3条回答
  • 2020-12-02 14:25

    OK, I got it figured out. since I needed the page view controller to be the root and handle everything, I subclassed UIPageViewControlleras iiFreeman suggested and conformed to the delegate and data source protocols. I then set up the controller in viewDidLoad, and implemented the delegate and data source methods. I added a property store an array of storyboard identifiers as well as one to keep track of the current index of that array. I also added a helper method to return the correct view controller given an index. I didn't need anything in my other tableView controller subclasses since my page view controller was handling everything. One thing I did realize is since my tableViewControllers were embedded in navigation controllers, my page view controller actually needed to show the navigation controllers, not the tableView controllers. below is my implementation:

    import Foundation
    import UIKit
    
    class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
    
        var index = 0
        var identifiers: NSArray = ["FirstNavigationController", "SecondNavigationController"]
        override func viewDidLoad() {
    
            self.dataSource = self
            self.delegate = self
    
            let startingViewController = self.viewControllerAtIndex(self.index)
            let viewControllers: NSArray = [startingViewController]
            self.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
    
    
    
        }
    
        func viewControllerAtIndex(index: Int) -> UINavigationController! {
    
            //first view controller = firstViewControllers navigation controller
            if index == 0 {
    
                return self.storyboard.instantiateViewControllerWithIdentifier("FirstNavigationController") as UINavigationController
    
            }
    
            //second view controller = secondViewController's navigation controller
            if index == 1 {
    
                return self.storyboard.instantiateViewControllerWithIdentifier("SecondNavigationController") as UINavigationController
            }
    
            return nil
        }
        func pageViewController(pageViewController: UIPageViewController!, viewControllerAfterViewController viewController: UIViewController!) -> UIViewController! {
    
            let identifier = viewController.restorationIdentifier
            let index = self.identifiers.indexOfObject(identifier)
    
            //if the index is the end of the array, return nil since we dont want a view controller after the last one
            if index == identifiers.count - 1 {
    
                return nil
            }
    
            //increment the index to get the viewController after the current index
            self.index = self.index + 1
            return self.viewControllerAtIndex(self.index)
    
        }
    
        func pageViewController(pageViewController: UIPageViewController!, viewControllerBeforeViewController viewController: UIViewController!) -> UIViewController! {
    
            let identifier = viewController.restorationIdentifier
            let index = self.identifiers.indexOfObject(identifier)
    
            //if the index is 0, return nil since we dont want a view controller before the first one
            if index == 0 {
    
                return nil
            }
    
            //decrement the index to get the viewController before the current one
            self.index = self.index - 1
            return self.viewControllerAtIndex(self.index)
    
        }
    
    
        func presentationCountForPageViewController(pageViewController: UIPageViewController!) -> Int {
            return self.identifiers.count
        }
    
        func presentationIndexForPageViewController(pageViewController: UIPageViewController!) -> Int {
            return 0
        }
    
    }
    

    this post helped me out a lot

    0 讨论(0)
  • 2020-12-02 14:29

    sure cause FirstViewController - initial view controller in your storyboard

    add a custom UIPageViewController subclass into your storyboard, mark it as initial and do all work inside, instantiate page controller members by storyboardIDs in your UIPageViewController subclass, do the same thing just inside another class

    0 讨论(0)
  • 2020-12-02 14:33

    Please be aware that this code will only work with two ViewController. If you are planning on using more you have to change the line:

    self.index = self.index + 1
    

    Found at the before and after call, to:

    self.index = index + 1
    

    To call the current ViewController index.

    0 讨论(0)
提交回复
热议问题