Is View recreated when i close an action sheet in swiftUI?

87 Views Asked by At

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.

1

There are 1 best solutions below

0
Sweeper On

Is View recreated when i close an action sheet in swiftUI?

First I'll assume "close an action sheet" means selecting "Cancel" on the action sheet. I'm not sure what loginViewModel.handleSignOut does 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.init is called? We can't be sure. It is not guaranteed that SwiftUI will call your init, nor is it guaranteed that it won't.

Do you mean ProfileView.body is accessed again? Seeing how you also passed $showLogoutOptions (which changed after the action sheet is dismissed) to LogoutButton, ProfileView.body will probably be evaluated again to see if anything in LogoutButton needs 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.body retuned 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.