1

I have one view on button click i am showing sheet using

.sheet(isPresented: $showView) {
            AddView(viewModel: AddViewModel(accessibilityID: accessibilityID,
                                                                                    showAddPassengerView: $showView),
                                             accessibilityID: accessibilityID)
        }

i am doing something in sheet and when button click i am dismissing sheet view and coming back to previous view..in this case i want to refresh my main view.

thank you for help.

New iOS Dev
  • 1,937
  • 7
  • 33
  • 67
  • Add `onDismiss` closure argument to sheet and update corresponding state/viewModel in it. – Asperi Oct 22 '21 at 12:54
  • @Asperi Thanks..can you please give small example how to refresh previous view – New iOS Dev Oct 22 '21 at 12:57
  • 1
    You can't "force" a refresh like you are thinking. You view refreshes when something changes. You did not post enough code for us to say what you can change to make your view refresh. If you use any wrapped property in your view(`@State`, `@Binding`, `@StateObject`, `@ObservedObject`, etc.) change one of those values will refresh the view. If nothing has changed, why would you need to refresh the view? – Yrb Oct 22 '21 at 13:01
  • i am updating something singleton in sheet view and that i using in main view for that i want to refresh view – New iOS Dev Oct 22 '21 at 13:03
  • As soon as you modify any dynamic property a view will be refreshed automatically, here is a demo of force-refresh if dependency is not dynamic (say some external computable properties) - https://stackoverflow.com/a/65127277/12299030. – Asperi Oct 22 '21 at 13:05
  • @state will observe change only in same file..how can i refresh previous view from sheet view? sorry but i am not clear – New iOS Dev Oct 22 '21 at 13:22
  • The `.onDisappear` would be in the same file on the linked view. – Yrb Oct 22 '21 at 13:24
  • in onDisappear do i need to change state from sheet view or main view? lil confused. ...if i change state from sheet view how will it refresh main view? – New iOS Dev Oct 22 '21 at 13:27

1 Answers1

1

Use onDismiss. Example:


 .sheet(isPresented: $showView) {
                  // task you want to perform after dismissing the sheet
              } content: {
                   AddView(viewModel: AddViewModel(accessibilityID: accessibilityID,
                                                                                    showAddPassengerView: $showView),
                                             accessibilityID: accessibilityID)
              }

Shahriar Nasim Nafi
  • 1,160
  • 15
  • 19