【问题标题】:"Type '[view]' cannot conform to 'View'" error when using NavigationLink()使用 NavigationLink() 时出现“类型 '[view]' 不能符合 'View'”错误
【发布时间】:2020-06-25 18:43:20
【问题描述】:

我有一个使用 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])
                
            }
        }
    }
}

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

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

【问题讨论】:

  • 错字 - SettingsView()

标签: swift swiftui watchkit watchos


【解决方案1】:

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

NavigationLink(destination: SettingsView)

收件人:

NavigationLink(destination: SettingsView())

【讨论】:

    猜你喜欢
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    • 2014-12-19
    • 1970-01-01
    相关资源
    最近更新 更多