I have a couple of cascading modals that need to be refreshed depending on some background processes. In order to achieve this, I have created a struct where I hold all the logic for the UI and I am calling a couple of SwiftUI views with UIHostingController.init(rootView: views).
The challenge comes when I would like to dismiss the view by clicking a button from the child view. I am trying to use @State and @Binding but binding is forcing me to init the variable inside the child view.
Here is the code for the child:
struct ResultViewSilence: View {
@Binding var isDismissView: Bool
var hasSilence: Bool
let photolibrary = PhotoLibrary()
init(hasSilence: Bool) {
self.hasSilence = hasSilence
<--- here is where is asking to initialize isDismissView, but it should not be needed
}
I was able to init isDismissView, by doing this...
init(hasSilence: Bool, isDismissView: Binding<Bool>?) {
...
self._isDismissView = isDismissView!
but if I do that then it would break in the parent as I cannot pass the @State as a parameter in the UIHostingController and it would be required.
The error I would get if I do this is:
"Accessing State's value outside of being installed on a View. This will result in a constant Binding of the initial value and will not update."
I am also checking the @State variable and is not changing even if the button is triggered.
Cheers,
init(hasSilence: Bool)initialiser and createResultViewSilencein parent with both arguments according to type.ResultViewSilence(hasSilence: isSilence, isDimissView: $isDismissView)as it $isDismissView - self is used before all stored properties are initialized.