Go to a new view using SwiftUI

前端 未结 12 1856
走了就别回头了
走了就别回头了 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:02

    You can no longer use NavigationButton. Instead you should use NavigationLink.

    struct ContentView: View {
        var body: some View {
            NavigationView {
                NavigationLink(destination: DetailView()) {
                    Text("Push new screen")
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-08 20:16

    I think this is the easiest and clear way. Use fullScreenCover after UI tool.

    Button(action: {
             //code
             }){
               Text("Send")
             }.fullScreenCover(isPresented: self.$model.goToOtherView, content: {
                        OtherView()
                    })
    
    0 讨论(0)
  • 2020-12-08 20:17

    Here's another way to present a view WITHOUT using NavigationView. This is like UIKit's UIModalPresentationStyle.currentContext.

    struct PresenterButtonView: View {
    var body: some View {
        PresentationButton(Text("Tap to present"),
                           destination: Text("Hello world"))
    }}
    
    0 讨论(0)
  • 2020-12-08 20:17

    Now we can use NavigationLink


    File A:

    struct ContentView: View {
    var body: some View {
        NavigationView {
            VStack {
                Text("Hello World")
                NavigationLink(destination: secondView()) {
                    Text("Hit Me!")
                        .fontWeight(.semibold)
                        .font(.title)
                        .padding()
                        .foregroundColor(.white)
                        .background(LinearGradient(gradient: Gradient(colors: [Color(.white),Color(.blue)]), startPoint: .leading, endPoint: .trailing))
                        .cornerRadius(40)
            }
          }
        }
      }
    }
    

    File B:

    struct secondView: View {
    var body: some View {
        VStack {
        VStack(alignment: .leading) {
            Text("Turtle Rock")
                .font(.title)
            HStack(alignment: .top) {
                Text("Joshua Tree National Park")
                    .font(.subheadline)
                Spacer()
                Text("California")
                    .font(.subheadline)
            }
        }
        .padding()
            Spacer()
    
        }
      }
    }                                                                                                  
    
    0 讨论(0)
  • 2020-12-08 20:18

    if don't want to show the navigationView you can hide it in destination.

    struct ContentViewA : View {
        var body: some View {
            NavigationView {
                VStack {
                    Text("Hello World")
                    NavigationLink(destination: ContentViewB()) {
                        Text("Go To Next Step")
                    }
                }
            }
        }
    }
    
    
    
    struct ContentViewB : View {
            var body: some View {
                NavigationView {
                    VStack {
                        Text("Hello World B")
    
                    }.navigationBarTitle("")
                    .navigationBarHidden(true)
                }
            }
        }
    

    or if you want to hide it based on conditions you can use @State for changing the visibility.

    0 讨论(0)
  • 2020-12-08 20:18

    Have to create a DetailView like LandmarkDetail() and call a NavigationButton with destination as LandmarkDetail(). Now detail view was open.

    For passing values to detail screen means. by sending like below code.

    struct LandmarkList: View {
        var body: some View {
            NavigationView {
                List(landmarkData) { landmark in
                    NavigationButton(destination: LandmarkDetail()) {
                        LandmarkRow(landmark: landmark)
                    }
                }
                .navigationBarTitle(Text("Landmarks"))
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题