我有一个@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
基于围绕某些逻辑的已发布值的自定义关闭操作的视图,如下所示:
.onAppear(perform: {
viewModel.cancellable = viewModel.$shouldPopBack.sink(receiveValue: { shouldPopBackToHome in
if shouldPopBackToHome {
presentationMode.wrappedValue.dismiss()
}
})
})
此视图还有另一个选项来显示工作表
Button(action: {
shouldNavigateToQRCodeScanner = true
}, label: {
Text("Scan QR code")
.padding(.horizontal)
}).sheet(isPresented: $shouldNavigateToQRCodeScanner, content: {
QRCodeScannerView()
})
@Environment(\.presentationMode)
这里的问题是当我QRCodeScannerView
被初始化两次时,当我删除presentationMode时它工作正常。
更新 我在一个测试项目中尝试了同样的行为,它是一样的
struct ContentView: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
@State var isPresentd: Bool = false
var body: some View {
NavigationView {
Button(action: {
isPresentd = true
}, label: {
Text("Navigate to Second View")
})
.sheet(isPresented: $isPresentd, content: {
SecondView()
})
}
}
}
struct SecondView: View {
init() {
print("Initialized")
}
var body: some View {
Text("Second View")
}
}
它有同样的问题