ios – Can’t reload next ViewController in PageViewController

[ad_1]

I can’t update next page in my PageViewController.

  1. I add the required number of controllers, but the pageController.setViewControllers([controllers[0]], direction: .forward, animated: false) method leaves us with one.

  2. I have information for each next controller taken from the viewModel and updated when the currentIndex counter changes

  3. In the viewControllerBefore and viewControllerAfter methods, I initialize my view controller and increase or decrease currentIndex when scrolling forward or backward

  4. After that, the didSet in the view controller works for me and the update takes place.

  5. 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()

[ad_2]

Source link

Leave a Reply

Your email address will not be published.