I can’t update next page in my PageViewController.
-
I add the required number of controllers, but the
pageController.setViewControllers([controllers[0]], direction: .forward, animated: false)
method leaves us with one. -
I have information for each next controller taken from the viewModel and updated when the currentIndex counter changes
-
In the viewControllerBefore and viewControllerAfter methods, I initialize my view controller and increase or decrease currentIndex when scrolling forward or backward
-
After that, the didSet in the view controller works for me and the update takes place.
-
But, in the end, instead of 8 controllers, I only see 2.
PageViewController
class PageViewController: UIViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
let viewModel: GeneralViewModel
let locationViewModel: LocationViewModel
let realm = try! Realm()
var pageControl = UIPageControl.appearance()
var pageController: UIPageViewController!
var controllers = [UIViewController]()
var pendingIndex = 0
init(viewModel: GeneralViewModel, locationViewModel: LocationViewModel) {
self.viewModel = viewModel
self.locationViewModel = locationViewModel
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
pageController = UIPageViewController(transitionStyle: .scroll,
navigationOrientation: .horizontal,
options: nil)
pageController.delegate = self
pageController.dataSource = self
addChild(pageController)
view.addSubview(pageController.view)
let views = ["pageController": pageController.view] as [String: AnyObject]
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[pageController]|", options: [], metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[pageController]|", options: [], metrics: nil, views: views))
for _ in realm.objects(Cities.self) {
let mainScreenViewController = MainScrenenViewController(viewModel: self.viewModel, locationViewModel: self.locationViewModel)
self.controllers.append(mainScreenViewController)
}
pageController.setViewControllers([controllers[0]], direction: .forward, animated: false)
setupPageControl()
}
func setupPageControl() {
let realmCities = realm.objects(Cities.self)
pageControl = UIPageControl(frame: CGRect(x: 0,y: 100,width: UIScreen.main.bounds.width,height: 50))
pageControl.numberOfPages = realmCities.count
pageControl.tintColor = UIColor.lightGray
pageControl.pageIndicatorTintColor = UIColor.lightGray
pageControl.currentPageIndicatorTintColor = UIColor.black
pageControl.backgroundColor = UIColor.clear
view.addSubview(pageControl)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
if let index = controllers.firstIndex(of: viewController) {
if index > 0 {
let mainScreenViewController = MainScrenenViewController(viewModel: self.viewModel, locationViewModel: self.locationViewModel)
mainScreenViewController.currentIndex -= 1
controllers.append(mainScreenViewController)
return controllers[index - 1]
} else {
return nil
}
}
return nil
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
if let index = controllers.firstIndex(of: viewController) {
if index < controllers.count - 1 {
let mainScreenViewController = MainScrenenViewController(viewModel: self.viewModel, locationViewModel: self.locationViewModel)
mainScreenViewController.currentIndex += 1
controllers.append(mainScreenViewController)
return controllers[index + 1]
} else {
return nil
}
}
return nil
}
MainScreenViewController
class MainScrenenViewController: UIViewController, ChangeWeatherDelegate {
let viewModel: GeneralViewModel
let locationViewModel: LocationViewModel
var currentIndex = 0 {
didSet {
mainCollectionView.reloadData()
todayCollectionView.reloadData()
weekCollectionView.reloadData()
}
}
//MARK: -Realm
let realm = try! Realm()
2
Answers
In
viewDidLoad()
inPageViewController
, you are doing this:so, you’ve created an array of 8
MainScreenViewController
objects.However, you haven’t set
.currentIndex
on them, so they currently are allcurrentIndex = 0
.Then, in
viewControllerBefore
andviewControllerAfter
, instead of getting the already created view controller from yourcontrollers
array, you are *creating another instance ofMainScreenViewController
and setting its.currentIndex
to either-= 1
or+= 1
… but then you’re appending it tocontrollers
.Your code then returns a controller at
index - 1
orindex + 1
from the controllers array.It may help if you start a bit simpler… get your
UIPageViewController
working… and then add in the code for your view and location models.Here’s a quick example – based on your code – that adds a centered label. showing the "index" of each page:
You have already created your view controllers in viewDidLoad then why you are again recreating them in viewControllerBefore & viewControllerAfter ?, in viewControllerBefore & viewControllerAfter you just need to access already created view controllers, I have updated you code.