Reputation: 11
My problem is that TabView is not updating.
I want to log-in and then re-render another view (Profile Screen) instead of (Login Screen) in the same TabItem
.
The TabView
goes to ProfileScreen
only after i kill the app and reopen it.
Here's the TabView
code:
import SwiftUI
struct ContentView: View {
@State var userToken: String = UserDefaults.standard.string(forKey: "UserToken") ?? ""
var body: some View {
TabView{
HomeScreen(text: .constant("")).tabItem({ Image(systemName: "house") }).tag(0)
Text("Cart").tabItem({ Image(systemName: "cart") }).tag(1)
if userToken.isEmpty {
LoginScreen().tabItem({ Image(systemName: "person") }).tag(2)
}
else {
ProfileScreen().tabItem({ Image(systemName: "person") }).tag(2)
}
}
}
}
Things I have tried:
Upvotes: 1
Views: 642
Reputation: 629
If you are modifying the value of userToken in any other view, for example may be LoginView, you need a mechanism to inform the content view about that update.
You can consider using LocalNotifications here for sending the update to ContentView so that the view reloads on status update.
Upvotes: 0
Reputation: 93
If you're using the latest SwiftUI, consider using @AppStorage instead of @State:
@AppStorage("UserToken") var userToken: String = ""
Upvotes: 2