我在SwiftUI中设置了一个PageViewController
,按照已知的教程与UIKit交互使用了UIViewControllerRepresentable
等内容。
我的控制器数组包含简单的SwiftUI视图。我传递一个简单的IntroPage
结构体来提供内容。视图是嵌套的,这是良好的SwiftUI实践,如下所示:
PageView
- IntroScreen // part of the pages array
- VStack
- Text
- Image
- ButtonView // a separate view struct
- HStack
- ForEach // iterating through the buttons in my IntroPage Object
- Button
PageControl
现在我想在这些视图上添加一些按钮。它们应该可以通过我的
IntroPage
结构进行配置。其中一个按钮会前往到PageViewController中的下一页,另一个按钮会告诉PageViewController先添加更多页面,另一个按钮则会关闭整个PageViewController。我无法弄清楚如何访问PageViewController中的这些方法,以及在哪里实现它们(实例视图?PageViewController的协调对象?),以及如何访问我需要的对象(例如,PageViewController的
currentPage
绑定变量)。例如,我已经在PageViewController的协调器中实现了一个
forward()
函数:func forward() {
if parent.currentPage < parent.controllers.count - 1 {
parent.currentPage += 1
}
}
如果我在最终视图中在PageView旁边添加一个按钮,那么一切都很好,包括动画等等。但是我仍然不能从子视图中包含的按钮调用这个。
有什么想法吗?
编辑:根据要求,在ButtonView中存在此情况。
struct IntroButtonView: View {
var page: IntroPage
var body: some View {
HStack() {
Button(action:dismiss) {
Text("LocalizedButtonTitle")
}
// ... more buttons, based on certain conditions
}
}
func dismiss() {
// how to dismiss the modally presented controller ?
}
func next() {
// how to advance to the next page
}
func expand() {
// how to add more pages to the pages array
}
}
或许我完全错误地在以“事件”而不是“声明”为基础来思考......