import SwiftUI
struct ContentView: View {
@State var showSecond = false
@State var showThird = false
var body: some View {
VStack(spacing: 50) {
Text("FirstView")
Button("to SecondView") {
self.showSecond = true
}
.sheet(isPresented: $showSecond) {
VStack(spacing: 50) {
Text("SecondView")
Button("to ThirdView") {
self.showThird = true
}
.sheet(isPresented: self.$showThird) {
VStack(spacing: 50) {
Text("ThirdView")
Button("back") {
self.showThird = false
}
Button("back to FirstView") {
self.showThird = false
self.showSecond = false
}
}
}
Button("back") {
self.showSecond = false
}
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
上述代码从FirstView过渡到SecondView,从SecondView过渡到ThirdView。在SecondView和ThirdView的“返回”按钮正常返回上一个屏幕。
然而,如果您在ThirdView中点击“返回到FirstView”按钮,则会显示SecondView而不返回到FirstView。 在此操作之后,当您点击SecondView的“返回”按钮时,它不会返回到FirstView。
如何直接从ThirdView返回到FirstView?
2020年2月19日添加
我已根据答案添加了解决方案代码。
解决方案1:基于Asperi的计划A。
struct ContentView: View {
@State var showSecond = false
@State var showThird = false
var body: some View {
VStack(spacing: 50) {
Text("FirstView")
Button("to SecondView") {
self.showSecond = true
}
.sheet(isPresented: $showSecond) {
VStack(spacing: 50) {
Text("SecondView")
Button("to ThirdView") {
self.showThird = true
}
.sheet(isPresented: self.$showThird) {
VStack(spacing: 50) {
Text("ThirdView")
Button("back") {
self.showThird = false
}
Button("back to FirstView") {
DispatchQueue.main.async {
self.showThird = false
DispatchQueue.main.async {
self.showSecond = false
}
}
}
}
}
Button("back") {
self.showSecond = false
}
}
}
}
}
}
解决方案2:基于Asperi的计划B。
struct ContentView: View {
@State var showSecond = false
@State var showThird = false
@State var backToFirst = false
var body: some View {
VStack(spacing: 50) {
Text("FirstView")
Button("to SecondView") {
self.showSecond = true
}
.sheet(isPresented: $showSecond) {
VStack(spacing: 50) {
Text("SecondView")
Button("to ThirdView") {
self.showThird = true
}
.sheet(isPresented: self.$showThird, onDismiss: {
if self.backToFirst {
self.showSecond = false
}
}) {
VStack(spacing: 50) {
Text("ThirdView")
Button("back") {
self.showThird = false
self.backToFirst = false
}
Button("back to FirstView") {
self.showThird = false
self.backToFirst = true
}
}
}
Button("back") {
self.showSecond = false
}
}
}
}
}
}
解决方案3:基于Joseph的建议。
struct ContentView: View {
@State var showSecond = false
@State var showThird = false
var body: some View {
GeometryReader { geometry in
ZStack {
VStack(spacing: 50) {
Text("FirstView")
Button("to SecondView") {
self.showSecond = true
}
}
.frame(width: geometry.size.width, height: geometry.size.height)
.background(Rectangle().foregroundColor(.white))
if self.showSecond {
VStack(spacing: 50) {
Text("SecondView")
Button("to ThirdView") {
self.showThird = true
}
Button("back") {
self.showSecond = false
}
}
.frame(width: geometry.size.width, height: geometry.size.height)
.background(Rectangle().foregroundColor(.white))
if self.showThird {
VStack(spacing: 50) {
Text("ThirdView")
Button("back") {
self.showThird = false
}
Button("back to FirstView") {
self.showThird = false
self.showSecond = false
}
}
.frame(width: geometry.size.width, height: geometry.size.height)
.background(Rectangle().foregroundColor(.white))
}
}
}
}
}
}