1 | initial version |
This may be because the .sheet view is not connected to the main view's session. To fix this issue, you can pass a binding or an environment object to the .sheet view, which contains information about the user's session. You can then use this information to log out the user.
Here's an example of passing a binding to the .sheet view:
struct ContentView: View {
@State private var isLoggedIn = false
@State private var showSheet = false
var body: some View {
VStack {
if isLoggedIn {
Text("App content")
} else {
Button("Log in", action: { isLoggedIn = true })
}
Button("Log out", action: { isLoggedIn = false })
.sheet(isPresented: $showSheet, content: {
LogoutView(isLoggedIn: $isLoggedIn)
})
Button("Show sheet", action: { showSheet = true })
}
}
}
struct LogoutView: View {
@Binding var isLoggedIn: Bool
var body: some View {
VStack {
Text("Are you sure you want to log out?")
Button("Log out", action: { isLoggedIn = false })
}
}
}
In this example, we have a ContentView
that contains a button to log out the user, and a button to show a .sheet
view. The .sheet
view shows a LogoutView
, which contains a button to log out the user.
To pass the isLoggedIn
state to the LogoutView
, we declare it as a @Binding
in the LogoutView
and pass it as a parameter to the view's initializer. We then use this binding to update the isLoggedIn
state in the ContentView
when the user logs out.
By using a binding, we ensure that the LogoutView
is connected to the ContentView
's session, and the log out function works as expected.