【发布时间】:2019-12-24 09:06:32
【问题描述】:
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))
}
}
}
}
}
}
【问题讨论】: