SwiftUI-将工作表转移到另一个视图

时间:2020-06-11 21:49:26

标签: swift swiftui

在我关闭工作表时,我试图导航到另一个视图,但是没有导航到原始视图

 @State var showOnboarding: Bool = false
 Button(action: {
                    self.showOnboarding.toggle()
                }) {
                    Text("Click me")

                }.sheet(isPresented: $showOnboarding) {

                    DiscoverView(showOnboarding: self.$showOnboarding)

                }

我的问题:可以放入.onDissapear {NewView()}之类的东西吗?

1 个答案:

答案 0 :(得分:1)

如果我正确地理解了您的问题,则可以在离开工作表时使用onDismiss来执行操作:

.sheet(isPresented: $showOnboarding, onDismiss: {
    // on dismiss
    // here you can set some variables for presenting another sheet or navigating to some other views
}) {
    DiscoverView(showOnboarding: self.$showOnboarding)
}

您可以使用带有NavigationLink参数的isActive以编程方式显示另一个视图:

NavigationLink(destination: NewView(), isActive: $linkActive) {
    EmptyView()
}

总结代码如下:

struct ContentView: View {
    @State var showOnboarding: Bool = false
    @State var linkActive: Bool = false

    var body: some View {
        NavigationView {
            VStack {
                Button(action: {
                    self.showOnboarding.toggle()
                }) {
                    Text("Click me")
                }
                NavigationLink(destination: NewView(), isActive: $linkActive) {
                    EmptyView()
                }
            }
        }.sheet(isPresented: $showOnboarding, onDismiss: {
            self.linkActive = true
        }) {
            DiscoverView(showOnboarding: self.$showOnboarding)
        }
    }
}