I have set up a PageViewController
in SwiftUI, following the known Tutorial Interfacing with UIKit, with UIViewControllerRepresentable
etc.
My array of controllers consists of simple SwiftUI views. I pass a simple IntroPage
struct to supply the content. The views are nested, as is good SwiftUI practice, thus:
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
Now I want to include some buttons on these views. They should be configurable via my IntroPage
struct. One of them advances to the next page in the PageViewController, another tells the PageViewController to add more pages first, another button dismisses the entire PageViewController.
I cannot figure out, how get access to these methods in the PageViewController, where to implement them (the instance view? the coordinator of the PageViewController?) and how to reach the objects I need (e.g. the currentPage
Binding variable of the PageViewController).
For example, I have implemented a forward()
function in the PageViewController's coordinator:
func forward() {
if parent.currentPage < parent.controllers.count - 1 {
parent.currentPage += 1
}
}
...which works fine, with animations and all, if I add a button right beside the PageView on my final View. But I still cannot call this from the button contained in the child view.
Any ideas?
EDIT: Upon request, here is a situation in the 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
}
}
Or maybe I am completely wrong and still think in terms of "events" rather than "declaration"...