I Have this following code:
struct ProfileView: View {
@ObservedObject var loginViewModel: LoginViewModel
@State private var showLogoutOptions = false
var body: some View {
NavigationView {
VStack {
}
.toolbar {
ToolbarItem(placement: .topBarLeading) {
LogoutButton(showLogoutOptions: $showLogoutOptions)
}
}
.actionSheet(isPresented: $showLogoutOptions) {
ActionSheet(
title: Text("Settings"),
message: Text("What you want to do?"),
buttons: [
.destructive(
Text("Sign Out"),
action: loginViewModel.handleSignOut
), .cancel()
]
)
}
} }
}
struct LogoutButton: View {
@Binding var showLogoutOptions: Bool
var body: some View {
Button {
showLogoutOptions = true
} label: {
Image(systemName: "gear")
.font(.system(size: 24))
.bold()
.foregroundStyle(.black)
}
}
}
And when i dismiss the action sheet, the showLogoutOptions is false again. So, is my view recreated when i dismiss the action sheet or another proccess runs internally?
I tried to read apple's documentation but found nothing about it.
First I'll assume "close an action sheet" means selecting "Cancel" on the action sheet. I'm not sure what
loginViewModel.handleSignOutdoes so can't really reason about it.Then, we need to figure out what it means for the "View" to be "recreated".
Do you mean
ProfileView.initis called? We can't be sure. It is not guaranteed that SwiftUI will call yourinit, nor is it guaranteed that it won't.Do you mean
ProfileView.bodyis accessed again? Seeing how you also passed$showLogoutOptions(which changed after the action sheet is dismissed) toLogoutButton,ProfileView.bodywill probably be evaluated again to see if anything inLogoutButtonneeds to be updated (and there isn't, in this situation).Or do you mean the buttons and navigation bar on screen change to a different button and navigation bar? They are probably not changed. SwiftUI compares what
ProfileView.bodyretuned last time, with what it returned this time, and finds that practically nothing has changed except the action sheet being dismissed, so it won't make a new button or a new navigation bar.