Skip to content

sheet(unwrapping:) shows a blank view #151

Answered by stephencelis
joshuatbrown asked this question in Q&A
Discussion options

You must be logged in to vote

Unfortunately this seems to be a SwiftUI bug with @State and derived bindings. If you change to use an observable object it works fine:

class FeatureModel: ObservableObject {
  @Published var presentedValue: String?
}

struct FeatureView: View {
  @ObservedObject var model = FeatureModel()

  var body: some View {
    Button("Show sheet") {
      self.model.presentedValue = "Hello!"
    }
    .sheet(unwrapping: self.$model.presentedValue) { $value in
      TextField("Value", text: $value)
    }
  }
}

It also works fine with the newer @Observable macro and @Bindable:

@Observable
class FeatureModel {
  var presentedValue: String?
}

struct FeatureView: View {
  @Bindable var model = Feature…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@stephencelis
Comment options

@joshuatbrown
Comment options

Answer selected by joshuatbrown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
apple bug Something isn't working due to a bug on Apple's platforms.
2 participants
Converted from issue

This discussion was converted from issue #150 on May 11, 2024 05:52.