更改 UIPageViewController 中显示的初始视图控制器

Changing Initial View Controller Shown in UIPageViewController

我几个月前才开始使用 swift 编程,目前我正在做一些随机项目来帮助我更好地学习这门语言。目前,我正在尝试通过使用 UIPageViewController 创建一个类似于 Snapchat 的 UI。水平滚动正在工作,我可以正确浏览页面,但我想更改显示的初始视图控制器。在下面的代码中,我有三个 ViewController(vc1、vc2、vc3)。我想让它们保持相同的顺序,但首先显示 vc2,所以基本上将它放在其他两个的中间。感谢所有帮助。谢谢!

import UIKit
import FirebaseDatabase
import FirebaseAuth
import Firebase

class RootPageViewController: UIPageViewController, UIPageViewControllerDataSource {

    let Cards = CardsVC()

    lazy var viewControllerList:[UIViewController] = {

        let sb = UIStoryboard(name: "Main", bundle: nil)

        let vc1 = sb.instantiateViewController(withIdentifier: "MessagesVC")
        let vc2 = sb.instantiateViewController(withIdentifier: "CardsVC")
        let vc3 = sb.instantiateViewController(withIdentifier: "ProfileVC")

        return[vc1, vc2, vc3]
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        //if Auth.auth().currentUser?.uid == nil{
            //logout()
        //}else{
        self.dataSource = self

        if let firstViewController = viewControllerList.first{
            self.setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil)
        }
    //}
    }

    func logout(){
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let verifyPhoneVC = storyboard.instantiateViewController(withIdentifier: "enterPhoneNumber")
        present(verifyPhoneVC, animated: true, completion: nil)

    }
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

        guard let vcIndex = viewControllerList.index(of: viewController) else {return nil}
        let previousIndex = vcIndex - 1
        guard previousIndex >= 0 else {return nil}
        guard viewControllerList.count > previousIndex else {return nil}

        return viewControllerList[previousIndex]
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {

        guard let vcIndex = viewControllerList.index(of: viewController) else {return nil}
        let nextIndex = vcIndex + 1
        guard viewControllerList.count != nextIndex else {return nil}
        guard viewControllerList.count > nextIndex else {return nil}

        return viewControllerList[nextIndex]
    }

}

你已经准备好了整个代码,你需要做的就是从 viewDidLoad 中的 viewControllerList 传递 secondViewController 就这样了

所以替换

if let firstViewController = viewControllerList.first{
            self.setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil)
   }

let secondViewController = viewControllerList[1]
    self.setViewControllers([secondViewController], direction: .forward, animated: true, completion: nil)

希望对您有所帮助

这里我在 swift 3 中编写了一个扩展,用于在 UIPageViewController 中导航下一页和上一页。您可以根据需要进行修改。

扩展 UIPageViewController {

func goToNextPage(animated: Bool = true) {
    guard let currentViewController = self.viewControllers?.first else { return }
    guard let nextViewController = dataSource?.pageViewController(self, viewControllerAfter: currentViewController) else { return }
    setViewControllers([nextViewController], direction: .forward, animated: animated, completion: nil)
}

func goToPreviousPage(animated: Bool = true) {
    guard let currentViewController = self.viewControllers?.first else { return }
    guard let previousViewController = dataSource?.pageViewController(self, viewControllerBefore: currentViewController) else { return }
    setViewControllers([previousViewController], direction: .reverse, animated: animated, completion: nil)
}

}

希望您觉得它有用。 接受建议。