Skip to content

Remove onDismiss closure call when dismissing presentedViewController #238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

woohyunjin06
Copy link

onDismiss closure is being called even when the presentedViewController is dismissed.
As far as I understand, the onDismiss closure should only be called when the ViewController passed as the content closure is dismissed.

@stephencelis
Copy link
Member

@woohyunjin06 Thanks for taking the time to explore. I think that onDismiss is the behavior we want, though. That branch is dismissing the current view controller before presenting a new one for a new destination. The behavior matches SwiftUI:

import SwiftUI

struct Item: Identifiable {
  let id = UUID()
}

struct ContentView: View {
  @State var destination: Item?

  var body: some View {
    VStack {
      Button("Sheet") {
        destination = Item()
      }
    }
    .sheet(item: $destination) {
      print("onDismiss")
    } content: { item in
      Button("Another sheet") {
        destination = Item()
      }
    }
  }
}

#Preview {
  ContentView()
}

If you run the code above and tap "Sheet", and then tap "Another sheet", you will see "onDismiss" print in the console between the dismissal and presentation.

Since I believe the current behavior is what we want I'm going to close this out, but if I'm missing something or you can demonstrate an issue with a unit test, please let me know!

@woohyunjin06
Copy link
Author

Hi @stephencelis !
To demonstrate the issue, I modified the ConciseEnumNavigationViewController.swift as follows:

present(isPresented: UIBinding($model.destination.sheet1))  {
  print("onDismiss sheet1")
} content: {
  let vc = UIHostingController(
    rootView: Form {
      Text("Sheet 1")
    }
  )
  vc.mediumDetents()
  return vc
}
present(isPresented: UIBinding($model.destination.sheet2)) {
  print("onDismiss sheet2")
} content: {
  let vc = UIHostingController(
    rootView: Form {
      Text("Sheet 2")
      Button("Present Sheet 1") {
        self.model.destination = .sheet1
      }
    }
  )
  vc.mediumDetents()
  return vc
}

When running this code, you can see that onDismiss sheet1 is printed in console when presenting "Sheet 1" from "Sheet 2" even though Sheet 1 hasn’t actually been dismissed.
https://github.com/user-attachments/assets/9f3f64f1-a5f6-45a8-ae4e-a032669e6584

This behavior differs from the results when performing the same actions in SwiftUI.
https://github.com/user-attachments/assets/962f9b5e-9b3b-4cd5-99c1-4c57b9303a0f

it seems that simply removing the onDismiss call is not the right way to solve this issue, but I think the current behavior of onDismiss closure is definitely a bit weird.

Full code: https://gist.github.com/woohyunjin06/399e863766803ce64e68c2a76d74050f

@stephencelis
Copy link
Member

@woohyunjin06 Thanks for the repro! I understand the problem now and have captured it in a test here: #242

@woohyunjin06 woohyunjin06 deleted the on-dismiss-called branch October 21, 2024 04:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants