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
}
}
}
}
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