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