I was able to solve this issue by creating two separate @Published
bools in the ObservedObject
that each control sheet presentation in their respective View
. These values are passed into the View
which allow them to be manipulated outside of the View
which resolved the deep linking issue I alluded to.
As @xTwisteDx mentioned, the issue stemmed from the binding being flipped to false on the redraw. By assigning a passed in @State
property to the sheet binding, the binding was no longer getting flipped. Then all I had to do was create @Published
triggers in the ObservedObject
so I could turn them on when necessary in the deep linking class.
It's important to note that these two bools can never be on at the same time. So in setting one to true, the other must be set to false
As @workingdogsupportUkraine mentioned, it may not be correct to create a shared instance of the @ObservedObject
within itself, but passing the object in to consecutive Views was not an option for me.
So the working code looks like this:
TabView:
struct TabView: View {
@StateObject private var obsPresentation = ObsPresentation.shared
var body: some View {
ZStack {
VStack {
TabView(selection: $obsPresentation.currentTab) {
if obsPresentation.conditionOne {
HealthView(showSheet: obsPresentation.tiggerSheet1)
.tabItem {
Label("Person 1 Health", systemImage: "heart")
}
.tag(obsPresentation.Tab.health1)
}
if obsPresentation.conditionTwo {
HealthView(showSheet: obsPresentation.triggerSheet2)
.tabItem {
Label("Person 2 Health", systemImage: "heart")
}
.tag(obsPresentation.Tab.health2)
}
}
}
}
}
}
HealthView:
struct HealthView: View {
@State var showSheet: Bool
var body: some View {
VStack() {
ScrollView {
LazyVGrid() {
ForEach(...) { _ in
Button(action: {
showSheet = true
}) {
... UI
}
}
}
}
}
.sheet(isPresented: $showSheet) {
SheetView()
}
}
}
Observed Object:
public class ObsPresentation: ObservableObject {
public static var shared = ObsPresentation()
public enum Tab {
case health1, health2
}
@Published public var currentTab: Tab = .health1 {
didSet {
switch tabSelection {
case .health1:
...
case .health2:
...
default: break
}
}
}
// These conditions are manipulated elsewhere
@Published public var condition1 = true
@Published public var condition2 = true
@Published var triggerSheet1 = false
@Published var triggerSheet2 = false
}