我正在使用this教程中的PageViewController

我想传递一个函数,一旦用户达到最终索引,该函数将在协调器内运行.

这是我的代码:

页面视图控制器.敏捷的

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视图

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

每当我try 在协调器内调用onLast()时,都会出现错误:

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

所以我不知道该怎么做,或者如果可能的话,我会非常感谢您的帮助或指点,谢谢.

推荐答案

Coordinator是另一个类(只是内部类),您必须在parent上调用它,如

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

Swift相关问答推荐

如何在SWIFT中使用DiscardingTaskGroup获得任务结果

如何在visionOS中定制悬停效果区域

它是RxSwift中直接用于可扩展视图的有效的可扩展BehaviorRelay值?

SwiftUI 组合问题:通过 AppEventsManager 在 ViewModel 之间共享数据

按数组大小进行类型判断?

UIView.convert(_ point:to:)的意外行为

在Swift中如何将NSUrl转换为本地路径URL

Swift 包管理器 Package.resolved 文件末尾的版本是什么意思?

为什么 performSegue() 不调用 shouldPerformSegue()

SwiftUI Preview 不适用于 Core Data

如何让不同的组件拥有不同的动画?

如何让 UIKit 挤压你的视图,使其适合屏幕

VStack SwiftUI 中的动态内容高度

符合协议要求委托变量在 ios13 中可用

在 ViewController 的 UICollectionView 中拉取刷新

即使设置为从不也可以访问 iOS11 照片库

使用 DispatchTime.now() + float 延迟?

如何快速识别字符串中的大写和小写字符?

UIButton 标题左对齐问题 (iOS/Swift)

快速延迟加载属性