Go to a new view using SwiftUI

前端 未结 12 1864
走了就别回头了
走了就别回头了 2020-12-08 19:28

I\'ve got a basic view with a button using SwiftUI and I\'m trying to present a new screen/view when the button is tapped. How do I do this? Am I suppose to create a delegat

12条回答
  •  天命终不由人
    2020-12-08 20:27

    I think Jake's answer is the basic way to NextView.

    And I think the way bellow is a simple, formal, and dynamic way to try, if you really need to hit a BUTTON. According to Paul Hudson's Video, from 10'00" to 12'00".

    (should go to 12'00" to 15'00", if you want to go to different views by tapping different buttons.) (should go to 15'00" to 16'00", if you want to go to second view and go back automatically.) And more

    And here is the code example.

    import SwiftUI
    
    struct ContentView: View {
    
        @State var areYouGoingToSecondView: Bool // Step 2
    
        var body: some View {
            NavigationView{ // Step 1
    
                VStack {
    
                    // Step 3
                    NavigationLink(destination: YourSecondView(), isActive: $areYouGoingToSecondView) { EmptyView() }
    
    
                    Text("Hello World")
    
                    Button(action: {
                        self.areYouGoingToSecondView = true // Step 4
    
                    }) {
                        Text("Do Something (Go To Second View)")
                        .font(.largeTitle)
                        .fontWeight(.ultraLight)
                    }
                }
            }
        }
    }
    

提交回复
热议问题