How to pass a value when opening a volume
We already covered how to open a new volume by ID and how to open a window with a value. Opening a volume with an ID is no different than with a window.
Set up a window group to accept a value
We start at the App level by adding a new Window Group with a .volumeric window style. The id is set to FlowerVolume and we can specify the value type like so: for: FlowerItem.self . In the content closure, we get the value–which is passed as an optional binding–and pass it to Flower01Scene.
WindowGroup(id: "FlowerVolume", for: FlowerItem.self, content: { $value in
Flower01Scene(item: $value)
})
.windowStyle(.volumetric)Passing the value to openWindow
In the ContentView, we can call openWindow by passing an id and a FlowerItem.
Button(action: {
openWindow(id: "FlowerVolume", value: item)
}, label: {
HStack {
Text(item.flower)
Text(item.name)
}
.font(.largeTitle)
})The view Flower01Scene expects an optional binding of type FlowerItem. In this example it simply renders the specified flower in the volume with smaller versions around the corners.
struct Flower01Scene: View {
@Binding var item: FlowerItem?
var body: some View {
if let item = item {
Flower2D(flowerEmoji: item.flower)
.ornament(attachmentAnchor: .scene(.bottomLeadingBack)) {
Text(item.flower)
}
.ornament(attachmentAnchor: .scene(.bottomLeadingFront)) {
Text(item.flower)
}
// and so on for the other anchors...
} else {
Text("No Flowers Found!")
.font(.title)
.padding()
}
}
}When we click the button in a list view row, a new volume will be opened for our flower. These volumes are sort of like “document-based” windows on macOS. We can open a single unique window for each combination of an id and a value. Calling openWindow(id: "FlowerWindow", value: item) will bring the window to the foreground. This happens when there is an open volume with the matching id and value.
Sample code is available in Garden08 in the Step Into Example Projects repo.
Download the Xcode project with this and many more examples from Step Into Vision.
Some examples are provided as standalone Xcode projects. You can find those here.

Follow Step Into Vision