[英]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.