SwiftUI sheets and environment objects

@EnvironmentObject is handy for passing data between views in SwiftUI. Hacking With Swift has an excellent guide on how to use it, but when I followed it, one some of my views it just wasn’t working correctly.

The good news is the answer is really simple. The bad news is it took me about five hours of thinking I’d set up my Core Data models wrong to figure this out.

Sheets in SwiftUI don’t automatically inherit @EnvironmentObject. To work around this, just set the environment object manually when creating your sheet:

// SceneDelegate.swift
let someObject = SomeObject()
let contentView = ContentView().environmentObject(someObject)


// MyView.swift
struct MyView: View {
  @EnvironmentObject var someObject: SomeObject
  @State private var sheetIsPresented = false

  var body: some View {
    Button("Open sheet") {
      self.sheetIsPresented = true
    }.sheet(isPresented: $sheetIsPresented) {
      AnotherView().environmentObject(self.someObject)
    }
  }
}

Views inside your sheet will inherit the environment object fine, it’s just sheets that won’t. Think of sheets as parallel to your main scene with an entirely new view hierarchy.

Thanks for reading.

To get in touch, email me or find me on Mastodon or Twitter.

If you liked this post, you'll love my iOS apps. Check them out below.

Personal Best

Level up your workouts

Taylor's Version

Upgrade Taylor Swift songs in your playlists

SalaryPig

Meet Trevor, the salary-tracking pig

Taylor Swift Quiz

How well do you know Taylor Swift?

Football Quiz

Test your soccer knowledge