使用 NavigationLink() 时出现“类型 '[view]' 无法符合 'View'”错误

Quo*_*ran 1 swift watchkit watchos swiftui

我有一个使用 SwiftUI 构建的简单 Apple Watch 应用程序。我正在尝试添加一个按钮以从“我”转到“ContentViewSettingsView使用”NavigationLink

\n
struct ContentView: View {\n    \n    @EnvironmentObject var settings: SettingsObject\n    @EnvironmentObject var chilly: ChillyObject\n    \n    var body: some View {\n        ZStack{\n            VStack{\n\n                Text(chilly.message)\n                    .foregroundColor(chilly.textColor)\n                    .font(.largeTitle)\n                    .fontWeight(.heavy)\n                    .multilineTextAlignment(.center).fixedSize(horizontal: false, vertical: true)\n                    .padding(.top, 9.0)\n                            \n                Text(chilly.emoji)\n                    .font(.largeTitle).foregroundColor(Color.black)\n                    .multilineTextAlignment(.center)\n                    .padding(.bottom, -20.0)\n                \n                NavigationLink(destination: SettingsView) {\n                    Text("\xe2\x9a\x99\xef\xb8\x8f Settings")\n                        .font(.body)\n                        .fontWeight(.semibold)\n                }\n                .frame(width: 120.0)\n\n            }\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n
struct SettingsView: View {\n    \n    @EnvironmentObject var settings: SettingsObject\n            \n    var body: some View {\n        ZStack {\n            VStack {\n                Text("Threshold Temp: \\(Int(settings.thresholdTemperature))\xc2\xb0 \\(settings.thresholdUnits)")\n                    .fontWeight(.light)\n                \n                Slider(value: $settings.thresholdTemperature, in: 30...90, step: 1)\n                    .padding([.leading, .bottom, .trailing])\n                \n            }\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我收到此错误:Type 'SettingsView.Type' cannot conform to 'View'; only struct/enum/class types can conform to protocols在这一行:NavigationLink(destination: SettingsView) {在我的ContentView.

\n

我认为它是在我遵循教程并开始尝试使用@EnvironmentObject包装器而不是@ObservedObject包装器时引入的,但老实说我不能确定。任何见解都会很棒。谢谢!

\n

Bib*_*cob 5

您正在传递目标参数的类型SettingsView,您应该传递一个实例。修改一下:

NavigationLink(destination: SettingsView)
Run Code Online (Sandbox Code Playgroud)

到:

NavigationLink(destination: SettingsView())
Run Code Online (Sandbox Code Playgroud)