Reputation: 327
I am trying to goto another page from a sheet view. Here is the code for the first view:
struct FirstView: View {
@State fileprivate var isShowingSheet: Bool = false
var body: some View {
Button(action: {
isShowingSheet = true
}) {
Text("Show the sheet")
}
.sheet(isPresented: $isShowingSheet, content: {
SecondView()
})
}
}
And here is the code for the second view:
struct SecondView: View {
var body: some View {
NavigationLink(destination: ThirdView()) {
Text("Show the third view.")
}
}
}
The Problem is that the third view will also be in that sheet. Is it possible to make the third view a normal page instead of a view in a sheet?
Thank you!
Upvotes: 2
Views: 2067
Reputation: 258441
NavigationLink
should be in same view hierarchy of NavigationView
to work, but .sheet
introduces another different view hierarchy, so the solution would be to leave (hidden) navigation link in previous view, but activate it programmatically from view in sheet.
Here is a demo (tested with Xcode 12 / iOS 14)
struct FirstView: View {
@State fileprivate var isShowingSheet: Bool = false
@State private var showThirdView = false
var body: some View {
Button(action: {
isShowingSheet = true
}) {
Text("Show the sheet")
}
.sheet(isPresented: $isShowingSheet, content: {
SecondView(showNext: $showThirdView)
})
.background(
NavigationLink(destination: ThirdView(), isActive: $showThirdView) {
EmptyView()
}
)
}
}
struct SecondView: View {
@Environment(\.presentationMode) var presentationMode
@Binding var showNext: Bool
var body: some View {
Button("Show the third view.") {
self.presentationMode.wrappedValue.dismiss()
DispatchQueue.main.async {
self.showNext = true
}
}
}
}
Upvotes: 2