UIPageView 控制器。如何设置自定义 UIViewControllers 数组

UIPageViewController. How to set up the array of custom UIViewControllers

我有一组 4 个 UIViewController,都有自己的自定义 class,并且我设置了委托。截至目前,我的主视图中有容器视图,它们会滚动到屏幕外,这就是我做 "pseudo-page view controller" 的方式。但是,为了培训起见,我想使用 UIPageViewController 将其转换为实际的页面视图控制器。
我看过很多教程,但大多数人只使用一个视图控制器并更改其中的文本以显示多个视图控制器。在我的例子中,我有 4 个自定义视图控制器:

var glanceController: GlanceVC!
var goalsController: GoalsVC!
var commuteController: CommuteVC!
var compareController: CompareVC!

在我以前的伪页面视图控制器上,我这样初始化它们:

func loadVC() {
    glanceController = self.storyboard?.instantiateViewController(withIdentifier: "sbGlance") as? GlanceVC
    goalsController = self.storyboard?.instantiateViewController(withIdentifier: "sbGoals") as? GoalsVC
    commuteController = self.storyboard?.instantiateViewController(withIdentifier: "sbCommute") as? CommuteVC
    compareController = self.storyboard?.instantiateViewController(withIdentifier: "sbCompare") as? CompareVC
}

现在据我了解,UIPageViewController 需要一组 UIViewController。但是我无法创建这个数组:

var VCArray: [UIViewController] = [glanceController, goalsController, commuteController, compareController]

错误:无法在 属性 初始值设定项中使用实例成员 'glancecontrolleer'。

我尝试声明一个空白数组 var VCArray: [UIViewcontroller] = [] 然后在

之后添加
func loadVC() {
    glanceController = self.storyboard?.instantiateViewController(withIdentifier: "sbGlance") as? GlanceVC
    goalsController = self.storyboard?.instantiateViewController(withIdentifier: "sbGoals") as? GoalsVC
    commuteController = self.storyboard?.instantiateViewController(withIdentifier: "sbCommute") as? CommuteVC
    compareController = self.storyboard?.instantiateViewController(withIdentifier: "sbCompare") as? CompareVC
    VCArray += glanceController
}

然而这也有一个错误:"Argument Type @value glanceVC? does not conform to expected type "序列"。

有人可以帮我为我的 uipageview 制作这个自定义视图控制器数组吗?

创建计算 属性 和 return 视图控制器数组

var VCArray: [UIViewController] {
    if let glanceController = self.storyboard?.instantiateViewController(withIdentifier: "sbGlance") as? GlanceVC,
        let goalsController = self.storyboard?.instantiateViewController(withIdentifier: "sbGoals") as? GoalsVC,
        let commuteController = self.storyboard?.instantiateViewController(withIdentifier: "sbCommute") as? CommuteVC,
        let compareController = self.storyboard?.instantiateViewController(withIdentifier: "sbCompare") as? CompareVC {
            return [glanceController, goalsController, commuteController, compareController]
    }
    return []
}