【问题标题】:SwiftUI Combine: @Published is only available on properties of classesSwiftUI 组合:@Published 仅适用于类的属性
【发布时间】:2020-11-21 22:23:58
【问题描述】:

我有以下代码,但收到消息错误:

'wrappedValue' 不可用:@Published 仅适用于 类的属性

//*
/**
Chat
Created on 29/07/2020
*/

import SwiftUI

let lightGreyColor = Color(red: 239.0/255.0, green: 243.0/255.0, blue: 244.0/255.0, opacity: 1.0)

struct ConnectionView: View {
    @ObservedObject var keyboardResponder = KeyboardResponder()
    @ObservedObject var viewModel = ConnectionVM()
//    @State var uuid1: String = ""
//    @State var uuid2: String = ""
    @State var authenticationDidFail: Bool = false
    
    var body: some View {
        return VStack {
            WelcomeText()
            LogoImage()
            UUIDTextField(uuid: viewModel.uuid1)
            UUIDTextField(uuid: viewModel.uuid2)
            if authenticationDidFail {
                Text("Information not correct. Try again.")
                .offset(y: -10)
                .foregroundColor(.red)
            }
            Button(action: {
                print("Button tapped")
            }) {
               LoginButtonContent()
            }
        }
        .padding()
        .offset(y: -keyboardResponder.currentHeight*0.5)
    }
    struct WelcomeText : View {
        var body: some View {
            return Text("Welcome!")
                .font(.largeTitle)
                .fontWeight(.semibold)
                .padding(.bottom, 20)
        }
    }
    struct LogoImage : View {
        var body: some View {
            return Image("logo")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: 150, height: 150)
                .clipped()
                .cornerRadius(150)
                .padding(.bottom, 75)
        }
    }
    struct UUIDTextField : View {
        @Published var uuid: String
        var body: some View {
        return TextField("UUID", text: $uuid)
                    .padding()
                    .background(lightGreyColor)
                    .cornerRadius(5.0)
                    .padding(.bottom, 20)
            }
    }
    struct LoginButtonContent : View {
        var body: some View {
            return Text("LOGIN")
                .font(.headline)
                .foregroundColor(.white)
                .padding()
                .frame(width: 220, height: 60)
                .background(Color.green)
                .cornerRadius(15.0)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ConnectionView()
    }
}

我的问题是

如何通过参数将@Published var 传递给子视图。

更新问题

如果我使用绑定而不是发布,我会收到此错误:

无法将“字符串”类型的值转换为预期的参数类型“绑定”

【问题讨论】:

    标签: ios swiftui combine


    【解决方案1】:

    @Published 只能在类中使用,而子 View 是一个结构。您应该使用@Binding 而不是@Published 将绑定变量传递给子View

    struct UUIDTextField : View {
        @Binding var uuid: String
        var body: some View {
            return TextField("UUID", text: $uuid)
                .padding()
                .background(lightGreyColor)
                .cornerRadius(5.0)
                .padding(.bottom, 20)
        }
    }
    

    然后使用绑定到传递的参数。

    UUIDTextField(uuid: $viewModel.uuid1)
    UUIDTextField(uuid: $viewModel.uuid2)
    

    【讨论】:

    • 当我使用绑定而不是发布时出现此错误:无法将“字符串”类型的值转换为预期的参数类型“绑定”
    • 使用绑定。将$ 添加到变量中。检查更新。
    猜你喜欢
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 2021-01-31
    • 2011-05-14
    • 1970-01-01
    相关资源
    最近更新 更多