title: Use existing pages instead of initialised controllers in PageViewController
body: <p>I'm using a PageViewController from <a href="https://developer.apple.com/tutorials/swiftui/interfacing-with-uikit" rel="nofollow noreferrer">this</a> tutorial</p>
  <p>Since the pages are being added as controllers, here:</p>
  <p><code>controllers = parent.pages.map { UIHostingController(rootView: $0) }</code></p>
  <p>Whenever they're updated in my SwiftUI view, the user can't see any updates, since (i'm guessing) the <code>init</code> method doesn't refresh.</p>
  <p>So how am I able to use the current <code>pages</code> instead of instantiating a <code>controllers</code> array?</p>
  <p>Here is my code:</p>
  <p>PageViewController.swift</p>
  <pre><code>import SwiftUI
  import UIKit
  
  struct PageViewController&lt;Page: View&gt;: UIViewControllerRepresentable {
      var pages: [Page]
      var onLast: () -&gt; AnyView
      @Binding var currentPage: Int
  
      func makeCoordinator() -&gt; Coordinator {
          Coordinator(self)
      }
  
      func makeUIViewController(context: Context) -&gt; UIPageViewController {
          let pageViewController = UIPageViewController(
              transitionStyle: .scroll,
              navigationOrientation: .vertical)
          pageViewController.dataSource = context.coordinator
          pageViewController.delegate = context.coordinator
  
          return pageViewController
      }
  
      func updateUIViewController(_ pageViewController: UIPageViewController, context: Context) {
          pageViewController.setViewControllers(
              [context.coordinator.controllers[currentPage]], direction: .forward, animated: true)
      }
  
      class Coordinator: NSObject, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
          var parent: PageViewController
          var controllers = [UIViewController]()
  
          init(_ pageViewController: PageViewController) {
              parent = pageViewController
              controllers = parent.pages.map { UIHostingController(rootView: $0) }
          }
  
          func pageViewController(
              _ pageViewController: UIPageViewController,
              viewControllerBefore viewController: UIViewController) -&gt; UIViewController?
          {
              guard let index = controllers.firstIndex(of: viewController) else {
                  return nil
              }
              if index == 0 {
                  return controllers.last
              }
              return controllers[index - 1]
          }
  
          func pageViewController(
              _ pageViewController: UIPageViewController,
              viewControllerAfter viewController: UIViewController) -&gt; UIViewController?
          {
              guard let index = controllers.firstIndex(of: viewController) else {
                  return nil
              }
              if index + 1 == controllers.count {
                  return controllers.first
              }
              return controllers[index + 1]
          }
  
          func pageViewController(
              _ pageViewController: UIPageViewController,
              didFinishAnimating finished: Bool,
              previousViewControllers: [UIViewController],
              transitionCompleted completed: Bool) {
              if completed,
                 let visibleViewController = pageViewController.viewControllers?.first,
                 let index = controllers.firstIndex(of: visibleViewController) {
                  if index + 1 == controllers.count {
                      print(&quot;index: \(index)&quot;)
                      print(controllers.count)
                      controllers.append(UIHostingController(rootView: parent.onLast()))
                  }
                  parent.currentPage = index
              }
          }
      }
  }
  
  
  struct PageView&lt;Page: View&gt;: View, Equatable {
      static func == (lhs: PageView&lt;Page&gt;, rhs: PageView&lt;Page&gt;) -&gt; Bool {
          return true
      }
      var pages: [Page]
      var onLast: () -&gt; AnyView
      @Binding var currentPage: Int
  
      var body: some View {
          PageViewController(pages: pages, onLast: onLast, currentPage: $currentPage)
      }
  }
  </code></pre>
  <p>SwiftUI View</p>
  <pre><code>import SwiftUI
  
  struct Test: View {
      @State var color: Color = Color.blue
      
      var body: some View {
          PageView(
              pages: [
                  AnyView(
                      color
                  ),
                  AnyView(
                      Color.purple
                  ),
                  AnyView(Color.green),
                  AnyView(Color.orange),
              ],
               onLast: {
                      color = Color.red
                  },
               currentPage: $currentPage
          )
          .equatable()
      }
  }
  </code></pre>