【问题标题】:SwiftUI - How to dismiss back to root view and present new view?SwiftUI - 如何解散回根视图并呈现新视图?
【发布时间】:2022-07-12 03:02:09
【问题描述】:

这是我的堆栈;

FirstView (sheet) -> SecondView (NavigationLink) -> ThirdView

我想要的是从ThirdView 解散回FirstView 并从那里解散到sheet FourthView。

有什么想法吗?

【问题讨论】:

  • 想法和你的代码在同一个地方——在想象中????
  • 是的,但是缺乏知识会限制想象力!由于我是 SwiftUI 的新手,我不知道该怎么做,因此提出了这个问题。请收回反对票,如果你能提供帮助,请做!
  • 欢迎阅读,请花时间阅读How do I ask a good question?

标签: swiftui


【解决方案1】:

我认为实现上述功能的方法有很多,但我用最少的时间实现了以下。

因此,建议你在这里得到一个提示,并尝试用更好的方法自己实现它。

import SwiftUI

struct ContentView: View {
    
    @Environment(\.presentationMode) var presentationMode
    @AppStorage("shouldDismiss") var shouldDismiss: Bool = false
    @State private var isShowingSecondView = false
    @State private var isShowingForthView = false

    var body: some View {
        VStack {
            Text("FirstView")
            
            Button("Go to SecondView") {
                isShowingSecondView.toggle()
            }
            .sheet(isPresented: $isShowingSecondView) {
                SecondView()
                    .onDisappear(perform: {
                        if shouldDismiss {
                            presentationMode.wrappedValue.dismiss()
                            DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
                                isShowingForthView = true
                            }
                        }
                    })
            }
            .sheet(isPresented: $isShowingForthView) {
                ForthView()
            }
        }
    }
}

struct SecondView: View {
    
    @Environment(\.presentationMode) var presentationMode
    @AppStorage("shouldDismiss") var shouldDismiss: Bool = false
    
    var body: some View {
        NavigationView {
            VStack {
                Text("SecondView")
                
                NavigationLink(
                    "Go To ThirdView",
                    destination: ThirdView()
                        .onDisappear(perform: {
                            if shouldDismiss {
                                presentationMode.wrappedValue.dismiss()
                            }
                        })
                )
            }
        }
    }
}

struct ThirdView: View {
    
    @Environment(\.presentationMode) var presentationMode
    @AppStorage("shouldDismiss") var shouldDismiss: Bool = false
    
    var body: some View {
        VStack {
            Text("ThirdView")
            
            Button("Go to FirstView and open ForthView") {
                shouldDismiss = true
                presentationMode.wrappedValue.dismiss()
            }
        }
    }
}

struct ForthView: View {
    
    var body: some View {
        VStack {
            Text("ForthView")
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

【讨论】:

    猜你喜欢
    • 2020-03-16
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多