SwiftUI: How to present view when clicking on a button?

后端 未结 3 725
無奈伤痛
無奈伤痛 2020-12-05 01:45

I\'m trying to make an app using Apple\'s SwiftUI and I need to have two buttons that present two different views in a single List row.

3条回答
  •  孤街浪徒
    2020-12-05 02:21

    Much improved version (SwiftUI, iOS 13 beta 7)

    The same solution works for dismissing Modals presented with the .sheet modifier.

    import SwiftUI
    
    struct DetailView: View {
        @Environment(\.presentationMode) var presentationMode: Binding
        var body: some View {
            Button(
                "Here is Detail View. Tap to go back.",
                action: { self.presentationMode.wrappedValue.dismiss() }
            )
        }
    }
    
    struct RootView: View {
        var body: some View {
            VStack {
                NavigationLink(destination: DetailView())
                { Text("I am Root. Tap for Detail View.") }
            }
        }
    }
    
    struct ContentView: View {
        var body: some View {
            NavigationView {
                RootView()
            }
        }
    }
    

提交回复
热议问题