我正在使用 NavigationLink 在视图之间导航,它在 SwiftUI 的单个视图中不起作用
I am using NavigationLink to navigate between views and it doesn't work in a single view in SwiftUI
当我想退出我的应用程序时,我想使用 NavigationLink 显示具有 "log in" / "create account" 页面的初始视图。我在应用程序的其他地方使用了完全相同的方法并且它在那里工作,但在这里却没有。这是我的代码:
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
。
当我想退出我的应用程序时,我想使用 NavigationLink 显示具有 "log in" / "create account" 页面的初始视图。我在应用程序的其他地方使用了完全相同的方法并且它在那里工作,但在这里却没有。这是我的代码:
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
。