如何从NavigationView / NavigationLink返回到ContentView / RootView

时间:2020-01-30 20:30:09

标签: swiftui swiftui-navigationlink

我想在UserDefaults中设置一个标志值并导航到ContentView吗?问题是我无法在ContentView中按“是”导航,只有标志被切换,当我按“否”时,我仅返回到一个预览。

struct ContentView: View {

    @ObservedObject var appLaunch = AppLaunch()

    var body: some View {
        Group {
            if !appLaunch.isAppLaunchFirstTime {

                OnboardingNameView()

            }else {
                AppView()

            }
        }
    }
}

struct OnboardingResumeView: View {
    @ObservedObject var appLaunch = AppLaunch()
    var defaults = UserDefaults.standard
    @State private var userGender : String = ""


        var body: some View {

            VStack(alignment: .leading, spacing: 20) {
                Text(Metadata.OnboardingNavigationTitle._titleResumeView)
                .font(.system(.headline, design: .rounded))
                .fontWeight(.medium)
                    Text(Metadata.OnboardingResume._title)
                        .font(.system(.largeTitle, design: .rounded))
                    Text(Metadata.OnboardingResume._headline)
                        .font(.system(.headline, design: .rounded))
                List {
                    Text("Name: \(defaults.string(forKey: "name") ?? "not set")")
                    Text("Alter: \(defaults.string(forKey: "age") ?? "not set")")
                    Text("Gewicht: \(defaults.string(forKey: "weight") ?? "not set")")
                    Text("Größe: \(defaults.string(forKey: "height") ?? "not set")")
                    Text("Aktivitätslevel: \(defaults.string(forKey: "activity") ?? "not set")")
                    Text("Geschlecht: \(defaults.string(forKey: "gender") ?? "not set")")
                }
                HStack{
********** Here I want dismiss the NavigationView and go Back where the NavigationView begins **************  
                    NavigationLink(destination: OnboardingNameView()){
                        Text("No")
                        self.presentationMode.wrappedValue.dismiss()
                    }
                .padding()

                    Spacer()
********** Here I want go to the RootView / ContentView **************  
                    NavigationLink(destination: ContentView()){
                        Text("Yes")
                      .onTapGesture {
                      self.appLaunch.isAppLaunchFirstTime.toggle()
                      }
                    }
                .padding() 
                }
                }
                .navigationBarHidden(true)

            }
    }

0 个答案:

没有答案