SwiftUI How to push to next screen when tapping on Button

前端 未结 5 920
囚心锁ツ
囚心锁ツ 2020-12-21 18:02

I can navigate to next screen by using NavigationButton (push) or present with PresentationButton (present) but i want to push when i tap on Buttton()

Button         


        
5条回答
  •  南笙
    南笙 (楼主)
    2020-12-21 18:46

    You can use NavigationLink to implement this:

    struct DetailsView: View {
        var body: some View {
            VStack {
                Text("Hello world")
            }
        }
    }
    
    struct ContentView: View {
        @State var selection: Int? = nil
        var body: some View {
            NavigationView {
                VStack {
                    NavigationLink(destination: DetailsView(), tag: 1, selection: $selection) {
                        Button("Press me") {
                            self.selection = 1
                        }
                    }
                }
            }
        }
    }
    

提交回复
热议问题