3

当我想退出我的应用程序时,我想使用 NavigationLink 显示具有“登录”/“创建帐户”页面的初始视图。我在应用程序的其他地方使用了完全相同的方法,它在那里工作,但在这里它没有。这是我的代码:

import SwiftUI

var signOut = false

struct SignOut: View {

    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
    @State private var curent: Int? = nil

    var body: some View {
        VStack {
            NavigationLink(destination: ContentView(), tag: 1, selection: $curent) {
                EmptyView()
            }.buttonStyle(PlainButtonStyle())

            Button(action: {
                let defaults = UserDefaults.standard
                defaults.set(false, forKey: "isLoggedIn")
                defaults.set("", forKey: "token")

                if contentViewVerify
                {
                    print("content true")
                    self.presentationMode.wrappedValue.dismiss()
                }
                else if contentViewVerify == false
                {
                    print("content false")
                    self.curent = 1 // if contentViewVerify is false I want to use the NavigationLink method which activates once curent = 1
                }

            }) {
                Text("SignOut")
            }.navigationBarTitle("covid")
            .navigationBarBackButtonHidden(true)
            .onAppear(perform: {
                signOut = true
            })
        }
    }
}

struct SignOut_Previews: PreviewProvider {
    static var previews: some View {
        SignOut()
    }
}

先感谢您!

4

1 回答 1

1

NavigationLink works only in NavigationView, so it should be somewhere like

struct SignOut_Previews: PreviewProvider {
    static var previews: some View {
       NavigationView {       // for testing in preview
           SignOut()
       }
    }
}

Note: contentViewVerify changing is absent in presented code, make sure it is really set to false.

于 2020-03-19T17:16:29.567 回答