// NavigationState.swift
class NavigationState: ObservableObject {
@Published var contentViewNavigation = ContentViewNavigationState()
//@Published var selectedTab = 0
}
// App.swift
@main
struct SwiftUIDeepLinkApp: App {
@StateObject private var navigationAppState = NavigationState()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(navigationAppState)
.onAppear(perform: {
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
print("Dispatch called")
navigationAppState.contentViewNavigation.selectedTab = 1
//navigationAppState.selectedTab = 1
}
})
}
}
}
// ContentView.swift
class ContentViewNavigationState: ObservableObject {
@Published var selectedTab = 0
}
struct ContentView: View {
@EnvironmentObject var navigationState: NavigationState
var body: some View {
TabView(selection: $navigationState.contentViewNavigation.selectedTab) {
HomeContainerFlow1()
.tabItem { Text("Flow 1") }
.tag(0)
HomeContainerFlow2()
.embededInNavigation()
.tabItem { Text("Flow 2") }
.tag(1)
}.onReceive(navigationState.contentViewNavigation.$selectedTab, perform: { output in
print("Value changed: \(output)")
})
}
}
如您所见,我正在定义一个全局状态(“NavigationState”)来处理应用程序的导航(即选项卡选择)。因此,我有一个 ObservableObject (NavigationState),它将具有每个屏幕导航状态的属性(即 ContentViewNavigationState - 另一个 ObservableObject)。
实际行为
在这个例子中,我在 DispatchQueue.asyncAfter 中修改 App.swift 文件中的状态,状态正在改变(因为 ContentView 的 onReceive 修饰符正在显示更新值)但没有触发 ContentView 中的任何更新。
预期行为
应根据状态更改所选选项卡。
- 有趣的是,如果我还修改了另一个属性(即 commented
selectedTab
),在这种情况下会触发更新,但不是仅更新 contentViewNavigation 的属性时。