簡體   English   中英

我正在使用 NavigationLink 在視圖之間導航,但它在 SwiftUI 的單個視圖中不起作用

[英]I am using NavigationLink to navigate between views and it doesn't work in a single view in SwiftUI

當我想退出我的應用程序時,我想使用 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()
    }
}

先感謝您!

NavigationLink僅適用NavigationView ,所以它應該像

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

注意:呈現的代碼中不存在contentViewVerify更改,請確保它確實設置為false

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM