"Type '[view]' cannot conform to 'View'" 使用 NavigationLink() 时出错
"Type '[view]' cannot conform to 'View'" error when using NavigationLink()
我有一个使用 SwiftUI 构建的简单 Apple Watch 应用程序。我正在尝试使用 NavigationLink
添加一个按钮以从我的 ContentView
转到我的 SettingsView
struct ContentView: View {
@EnvironmentObject var settings: SettingsObject
@EnvironmentObject var chilly: ChillyObject
var body: some View {
ZStack{
VStack{
Text(chilly.message)
.foregroundColor(chilly.textColor)
.font(.largeTitle)
.fontWeight(.heavy)
.multilineTextAlignment(.center).fixedSize(horizontal: false, vertical: true)
.padding(.top, 9.0)
Text(chilly.emoji)
.font(.largeTitle).foregroundColor(Color.black)
.multilineTextAlignment(.center)
.padding(.bottom, -20.0)
NavigationLink(destination: SettingsView) {
Text("⚙️ Settings")
.font(.body)
.fontWeight(.semibold)
}
.frame(width: 120.0)
}
}
}
}
struct SettingsView: View {
@EnvironmentObject var settings: SettingsObject
var body: some View {
ZStack {
VStack {
Text("Threshold Temp: \(Int(settings.thresholdTemperature))° \(settings.thresholdUnits)")
.fontWeight(.light)
Slider(value: $settings.thresholdTemperature, in: 30...90, step: 1)
.padding([.leading, .bottom, .trailing])
}
}
}
}
我在 ContentView
.
这一行收到此错误:Type 'SettingsView.Type' cannot conform to 'View'; only struct/enum/class types can conform to protocols
:NavigationLink(destination: SettingsView) {
我认为它是在我按照教程开始尝试使用 @EnvironmentObject
包装器而不是 @ObservedObject
包装器时引入的,但老实说我不能确定。任何见解都会很棒。谢谢!
您正在为目标参数传递 SettingsView
类型,您应该传递一个实例。修改为:
NavigationLink(destination: SettingsView)
收件人:
NavigationLink(destination: SettingsView())
我有一个使用 SwiftUI 构建的简单 Apple Watch 应用程序。我正在尝试使用 NavigationLink
ContentView
转到我的 SettingsView
struct ContentView: View {
@EnvironmentObject var settings: SettingsObject
@EnvironmentObject var chilly: ChillyObject
var body: some View {
ZStack{
VStack{
Text(chilly.message)
.foregroundColor(chilly.textColor)
.font(.largeTitle)
.fontWeight(.heavy)
.multilineTextAlignment(.center).fixedSize(horizontal: false, vertical: true)
.padding(.top, 9.0)
Text(chilly.emoji)
.font(.largeTitle).foregroundColor(Color.black)
.multilineTextAlignment(.center)
.padding(.bottom, -20.0)
NavigationLink(destination: SettingsView) {
Text("⚙️ Settings")
.font(.body)
.fontWeight(.semibold)
}
.frame(width: 120.0)
}
}
}
}
struct SettingsView: View {
@EnvironmentObject var settings: SettingsObject
var body: some View {
ZStack {
VStack {
Text("Threshold Temp: \(Int(settings.thresholdTemperature))° \(settings.thresholdUnits)")
.fontWeight(.light)
Slider(value: $settings.thresholdTemperature, in: 30...90, step: 1)
.padding([.leading, .bottom, .trailing])
}
}
}
}
我在 ContentView
.
Type 'SettingsView.Type' cannot conform to 'View'; only struct/enum/class types can conform to protocols
:NavigationLink(destination: SettingsView) {
我认为它是在我按照教程开始尝试使用 @EnvironmentObject
包装器而不是 @ObservedObject
包装器时引入的,但老实说我不能确定。任何见解都会很棒。谢谢!
您正在为目标参数传递 SettingsView
类型,您应该传递一个实例。修改为:
NavigationLink(destination: SettingsView)
收件人:
NavigationLink(destination: SettingsView())