Home > Net >  Calling a function passed from SwiftUI view to PageViewController<Page>.Coordinator
Calling a function passed from SwiftUI view to PageViewController<Page>.Coordinator

Time:06-21

I'm using a PageViewController from this tutorial

And I would like to pass a function that will run inside the coordinator once the user has reached the final index.

Here is my code:

PageViewController.swift

import SwiftUI
import UIKit

struct PageViewController<Page: View>: UIViewControllerRepresentable {
    var pages: [Page]
    var onLast: () -> Void
    @Binding var currentPage: Int

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func makeUIViewController(context: Context) -> 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) -> UIViewController?
        {
            guard let index = controllers.firstIndex(of: viewController) else {
                return nil
            }
            if index == 0 {
                onLast()
                return controllers.last
            }
            return controllers[index - 1]
        }

        func pageViewController(
            _ pageViewController: UIPageViewController,
            viewControllerAfter viewController: UIViewController) -> 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) {
                parent.currentPage = index
            }
        }
    }
}


struct PageView<Page: View>: View, Equatable { // << confirm !!
    static func == (lhs: PageView<Page>, rhs: PageView<Page>) -> Bool {
        return true // just for demo, make it meaningful on some property
    }
    var pages: [Page]
    var onLast: () -> Void
    @Binding var currentPage: Int

    var body: some View {
        PageViewController(pages: pages, onLast: onLast, currentPage: $currentPage)
    }
}

struct PageView<Page: View>: View, Equatable { // << confirm !!
    static func == (lhs: PageView<Page>, rhs: PageView<Page>) -> Bool {
        return true // just for demo, make it meaningful on some property
    }
    var pages: [Page]
    var onLast: () -> Void
    @Binding var currentPage: Int

    var body: some View {
        PageViewController(pages: pages, onLast: onLast, currentPage: $currentPage)
    }
}

SwiftUI View

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()
    }
}

Whenever I try to call onLast() inside the Coordinator, I get the error:

Instance member 'onLast' of type 'PageViewController<Page>' cannot be used on instance of nested type 'PageViewController<Page>.Coordinator'

So not sure how to do this, or if it's possible, I'd appreciate any help or pointers, thank you.

CodePudding user response:

Coordinator is another class (just internal), you have to call it on parent, like

if index == 0 {
    parent.onLast()           // << here !!
    return controllers.last
}
  • Related