I have a set of 4 UIViewControllers, all have their own custom class, and I have delegates set up. As of now I have container views in my main view that get scrolled off-screen and thats how I'm doing a "pseudo-page view controller". However for the sake of training I wanted to convert this into an actual page-view controller using UIPageViewController.
I've seen a bunch of tutorials but most people just use one view controller and change the text inside it to display multiple ones. In my case I have 4 custom view controllers:
var glanceController: GlanceVC!
var goalsController: GoalsVC!
var commuteController: CommuteVC!
var compareController: CompareVC!
On my previous pseudo-page view controller I initializzed them like this:
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
}
Now from what I understand the UIPageViewController needs an array of UIViewControllers. However I can't create this array:
var VCArray: [UIViewController] = [glanceController, goalsController, commuteController, compareController]
error: Cannot use instance member 'glancecontrolleer' within propery initializer.
I tried instead declaring a blank array var VCArray: [UIViewcontroller] = [] and then adding after
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
}
however that also has an error: "Argument Type @value glanceVC? does not conform to expected type "Sequence".
Can someone help me make this array of custom view controllers for my uipageview?
Create a computed property and return the view controllers array
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 []
}