nachonavarro / Pages

📖 A lightweight, paging view solution for SwiftUI
MIT License
586 stars 55 forks source link

Parent.controllers is different than current.controllers. #3

Closed neHCuoHEpa closed 4 years ago

neHCuoHEpa commented 4 years ago

I have follow situation. struct HomeView { ... Pages(currentPage: self.$currentPage) { ChannelsView() PublicationsView() MarkedView() } ... The problem is coming from that in PublicationsView, Im realoading some data and view should be rendered again. Then "controllers" array is updated and refill with newest views. But in the Coordinator the parent reference keep the first "controllers" array. That follow to a bug: in "viewControllerBefore" and "viewControllerAfter" -> "parent.controllers.firstIndex(of: viewController)" is returning nil and the pages cannot be changed. I cannot find a easy solution. Could you help me?

neHCuoHEpa commented 4 years ago

I found a solution. Just need to update parent value of coordinator before any changes. Like this func updateUIViewController(_ pageViewController: UIPageViewController, context: Context) { context.coordinator.parent = self pageViewController.setViewControllers( [controllers[currentPage]], direction: .forward, animated: false ) }

jpmcglone-airside commented 4 years ago

For those reading along, update your updateUIViewController in PageViewController to:

func updateUIViewController(_ pageViewController: UIPageViewController, context: Context) {
    context.coordinator.parent = self
    pageViewController.setViewControllers(
        [controllers[currentPage]], direction: .forward, animated: false
    )
}

Worked for me!

nachonavarro commented 4 years ago

Awesome, thanks guys. I don't have time right now but I'll try to add it this weekend. If anybody wants to submit a pull request feel free :)

nachonavarro commented 4 years ago

Fixed