【发布时间】:2020-09-12 10:13:46
【问题描述】:
我在 SwiftUI 中设置了一个PageViewController,遵循已知的教程Interfacing with 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 的协调器?)以及如何访问我需要的对象(例如 currentPage Binding 变量PageViewController)。
比如我在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
}
}
或者也许我完全错了,仍然从“事件”而不是“声明”的角度思考......
【问题讨论】:
-
你能在代码中显示你需要调用什么函数吗?
-
为清晰起见添加了一些代码。
标签: ios swift swiftui uipageviewcontroller uiviewcontrollerrepresentable