admin 管理员组文章数量: 1086019
first of all thanks for attention and your help, so...
in my app I have router from where I log and logout user
@main
struct JobMatchApp: App {
@StateObject private var vm = RouterViewModel()
var body: some Scene {
WindowGroup {
NavigationStack {
Router()
}
.environmentObject(vm)
}
}
}
and from my Router I check if user is logged or not
struct Router: View {
EnvironmentObject var routerVM: RouterViewModel
var body: some View {
VStack {
if routerVM.isUserLoggedIn {
TabbarView()
} else {
LoginView()
}
}
.overlay {
if routerVM.isLoading {
VStack {
ProgressView()
.tint(.mintGreen)
.scaleEffect(1.5)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(.deepNavy)
}
}
}
}
like this it works in tabbar and login perfectly, but somewhere I use NavigationLink it not works any more. for example from view where I go with NavigationLink logout function doesn't work any more. but when I press back button user is logged out and I go to LoginView(), but not automatically I need to press back button. as I said it works perfectly where I dont use NavigationLink or new NavigationStack.
NavigationLink {
ProfileSettings().toolbar(.hidden)
} label: {
Image(systemName: "gearshape")
.renderingMode(.template)
.resizable()
.frame(width: 24, height: 24)
.foregroundStyle(.mintGreen)
}
any help?
first of all thanks for attention and your help, so...
in my app I have router from where I log and logout user
@main
struct JobMatchApp: App {
@StateObject private var vm = RouterViewModel()
var body: some Scene {
WindowGroup {
NavigationStack {
Router()
}
.environmentObject(vm)
}
}
}
and from my Router I check if user is logged or not
struct Router: View {
EnvironmentObject var routerVM: RouterViewModel
var body: some View {
VStack {
if routerVM.isUserLoggedIn {
TabbarView()
} else {
LoginView()
}
}
.overlay {
if routerVM.isLoading {
VStack {
ProgressView()
.tint(.mintGreen)
.scaleEffect(1.5)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(.deepNavy)
}
}
}
}
like this it works in tabbar and login perfectly, but somewhere I use NavigationLink it not works any more. for example from view where I go with NavigationLink logout function doesn't work any more. but when I press back button user is logged out and I go to LoginView(), but not automatically I need to press back button. as I said it works perfectly where I dont use NavigationLink or new NavigationStack.
NavigationLink {
ProfileSettings().toolbar(.hidden)
} label: {
Image(systemName: "gearshape")
.renderingMode(.template)
.resizable()
.frame(width: 24, height: 24)
.foregroundStyle(.mintGreen)
}
any help?
Share Improve this question asked Mar 27 at 12:55 the middlethe middle 1 3 |1 Answer
Reset to default 0Uses
NavigationPath
to control navigation programmatically.Resets navigation when
isUserLoggedIn
changes, ensuring it pops to the root.Use
NavigationPath
to programmatically manage the navigation stack and reset it on logout:
@State private var navigationPath = NavigationPath()
var body: some View {
NavigationStack(path: $navigationPath) {
VStack {
if routerVM.isUserLoggedIn {
TabbarView()
} else {
LoginView()
}
}
.onChange(of: routerVM.isUserLoggedIn) { isLoggedIn in
if !isLoggedIn {
navigationPath = NavigationPath() // Reset to root
}
}
}
}
本文标签: swiftNavigationLink in swiftui and routingStack Overflow
版权声明:本文标题:swift - NavigationLink in swiftui and routing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744088015a2531499.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Router
you should be using@EnvironmentObject var routerVM: RouterViewModel
. In addition,...somewhere I use NavigationLink it not works any more
, where do you use this, how do you call this, how do you pass theRouterViewModel
to this? Show a minimal reproducible code that produces your issue, see: minimal code. – workingdog support Ukraine Commented Mar 27 at 13:05TabBarView
andLoginView
(if required). This will simplify thenavigationDestination
modifier, and makes your app more modular, making the (valid) assumption, that a Login Scene and a TabBar Scene is completely different, not necessarily requiring a NavigationStack at all. – CouchDeveloper Commented Mar 27 at 13:21