I have a button in a view (inside a NavigationView
) that opens a full screen cover - a loading screen while some data is processing. When the cover is dismissed, I want to automatically route to the next view programmatically. I'm using a NavigationLink
with a tag and selection binding, and the binding value updates when the cover is dismissed, but the routing doesn't happen unless I tap that same "open modal" button again.
import SwiftUI
struct OpenerView: View {
@EnvironmentObject var viewModel: OpenerViewModel
@State private var selection: Int? = nil
@State private var presentLoadingScreen = false
var body: some View {
VStack {
NavigationLink(destination: SecondScreen(), tag: 1, selection: $selection) { EmptyView() }
Button(action: {
viewModel.frequency = 0
self.presentLoadingScreen.toggle()
}, label: {
Text("Open cover")
}).buttonStyle(PlainButtonStyle())
}
.navigationBarTitle("Nav title", displayMode: .inline)
.fullScreenCover(isPresented: $presentLoadingScreen, onDismiss: {
self.selection = 1
}, content: ModalView.init)
}
}
struct ModalView: View {
@Environment(.presentationMode) var presentationMode
var body: some View {
Text("Howdy")
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
presentationMode.wrappedValue.dismiss()
}
}
}
}
The first time I hit the Button, the cover opens. Inside the cover is just a DispatchQueue.main.asyncAfter
which dismisses it after 2 seconds. When it's dismissed, the onDismiss
fires, but then I have to hit the button again to route to SecondScreen
.
Any ideas?
Edit: Added the modal's View
question from:
https://stackoverflow.com/questions/65860539/navigationlink-doesnt-fire-after-fullscreencover-is-dismissed 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…