我目前正在为watchOS 6(独立应用)开发一个应用程序,使用XCode 11.5中的Swift/Swift on在macOS Catalina上使用。
在用户可以使用我的应用程序之前,需要一个配置过程。由于配置过程由几个相互显示的不同视图组成,所以我使用导航链接实现了这一点。
在配置过程完成后,用户应该点击一个按钮返回到“主”应用程序(主视图)。为了控制在同一层次上的视图,我的计划是使用一个EnvironmentObject (据我所知,一旦注入了一个EnvironmentObject,子视图就可以使用EnvironmentObject)与控制视图显示的“控制视图”相结合。因此,我遵循了教程:https://blckbirds.com/post/how-to-navigate-between-views-in-swiftui-by-using-an-environmentobject/
这是我的密码:
ContentView.swift
struct ContentView: View {
var body: some View {
ContentViewManager().environmentObject(AppStateControl())
}
}
struct ContentViewManager: View {
@EnvironmentObject var appStateControl: AppStateControl
var body: some View {
VStack {
if(appStateControl.callView == "AppConfig") {
AppConfig()
}
if(appStateControl.callView == "AppMain") {
AppMain()
}
}
}
}AppStateControl.swift
class AppStateControl: ObservableObject {
@Published var callView: String = "AppConfig"
}AppConfig.swift
struct AppConfig: View {
@EnvironmentObject var appStateControl: AppStateControl
var body: some View {
VStack {
Text("App Config Main")
NavigationLink(destination: DetailView1().environmentObject(appStateControl)) {
Text("Show Detail View 1")
}
}
}
}
struct DetailView1: View {
@EnvironmentObject var appStateControl: AppStateControl
var body: some View {
VStack {
Text("App Config Detail View 1")
NavigationLink(destination: DetailView2().environmentObject(appStateControl)) {
Text("Show Detail View 2")
}
}
}
}
struct DetailView2: View {
@EnvironmentObject var appStateControl: AppStateControl
var body: some View {
VStack {
Text("App Config Detail View 2")
Button(action: {
self.appStateControl.callView = "AppMain"
}) {
Text("Go to main App")
}
}
}
}AppMain.swift
struct AppMain: View {
var body: some View {
Text("Main App")
}
}在我的代码的前一个版本中(没有始终提交EnvironmentObject ),我得到了一个运行时错误(“线程1:致命错误:没有找到类型为ObservableObject的AppStateControl。作为该视图的祖先,AppStateControl的View.environmentObject(_:)可能会丢失。”由AppConfig.swift中的第41行引起。在互联网上,我读到这可能是NavigationLink的一个bug (参见:https://www.hackingwithswift.com/forums/swiftui/environment-object-not-being-inherited-by-child-sometimes-and-app-crashes/269,https://twitter.com/twostraws/status/1146315336578469888)。因此,建议将EnvironmentObject显式地传递到NavigationLink的目的地(上面的实现)。不幸的是,这也不起作用,只需单击"DetailView2“中的”转到主应用程序“按钮,就可以看到"DetailView1”而不是"AppMain“。
有什么办法解决这个问题吗?在我看来,通过导航链接调用的视图中的EnvironmentObject更改似乎不会刷新视图(正确)。
提前谢谢。
发布于 2020-07-01 18:44:32
解决方案之一是创建一个变量,控制是否显示导航堆栈。
class AppStateControl: ObservableObject {
...
@Published var isDetailActive = false // <- add this
}然后,可以通过设置NavigationLink参数来使用这个变量来控制第一个isActive。此外,还需要将.isDetailLink(false)添加到所有后续链接中。
堆栈中的第一个链接:
NavigationLink(destination: DetailView1().environmentObject(appStateControl), isActive: self.$appStateControl.isDetailActive) {
Text("Show Detail View 1")
}
.isDetailLink(false)所有其他链接:
NavigationLink(destination: DetailView2().environmentObject(appStateControl)) {
Text("Show Detail View 2")
}
.isDetailLink(false)然后将isDetailActive设置为false以弹出所有NavigationLinks并返回主视图:
Button(action: {
self.appStateControl.callView = "AppMain"
self.appStateControl.isDetailActive = false // <- add this
}) {
Text("Go to main App")
}https://stackoverflow.com/questions/62683093
复制相似问题