This issue is an initialization problem encountered in compiling code to transport variables between web views and views. I've asked 3 months ago(previous question was asked here.). I haven't been able to solve it yet. I gave up on solving and found for other alternatives, but wanted to know the cause or solution to the issue, so I re-identified the issue and recreated the question.
I'm implementing a sheet between a Web view and a View. The values in the sheet are always changing. The content of the sheet called from the view must be shared by the web view (or reverse). There is no problem when mounting the web view directly inside the view. But the issue occurs when using with 'let' expressions. The following error message appears right before compilation:
"...Cannot use instance member '$activeSheet' within property initializer; property initializers run before 'self' is available..."
I haven't only used init(){...} in the view and but also used various initializing methods, but the initialization error message does not disappear. The most cause of this issue seems to be a problem with init, which sends @State from the view to web view and requests the value of @Binding down from the Web view to View again.
Is there any way?
struct ItlnrView: View {
@State var activeSheet: **Bool** = false
.....
let webView = myWebView(web: nil, actSheet: **$activeSheet**, req: URLRequest(url: URL(string: "https://google.com")!)). <--- problem
var body: some View {
VStack {
webView
// myWebView(web: nil, actSheet: $activeSheet, req: URLRequest(url: URL(string: "https://google.com")!)) <--- no problem
}
.sheet(isPresented: $activeSheet) {
// code
}
}
}
struct myWebView: UIViewRepresentable {
let request: URLRequest
var webview: WKWebView?
@Binding var activeSheet: Bool
init(web: WKWebView?, actSheet: **Binding<Bool>**, req: URLRequest) {
self.webview = WKWebView()
self._activeSheet = actSheet
self.request = req
}
....
....
}
If I don't use let webView
and run myWebView(web:,actSheet:,req:)
in V/HStack directly, the sheet value is compatible/shared properly. But I must use/call by let webView
because I have to use the 'go back' and 'reload' functions of Web view. So whenever I use let webView
, the initialization error message occurs on $activeSheet.
Anyway, this Web view must be use instance member $activeSheet
within property initializer through let webView...
.
I hope you to be understand my lacked question.