“静态方法'buildBlock'要求'ToolbarItem<(), Button<some View>>'符合'View'

"Static method 'buildBlock' requires that 'ToolbarItem<(), Button<some View>>' conform to 'View'

提问人:meunomeecris 提问时间:11/15/2023 更新时间:11/17/2023 访问量:37

问:

静态方法“buildExpression”要求“ToolbarItem<(), Button>”符合“View”

public struct SetAStatusView: View {
    @ObservedObject var model: StatusViewModel
    @Environment(\.leafTheme) private var theme
    
    public init(model: StatusViewModel) {
        self.model = model
    }
    
    public var body: some View {
        NavigationView {
            VStack {
                InputStatus(model: model)
                Spacer()
            }
            .padding(.horizontal, 16)
            .toolbar {
                ToolbarItem(placement: .topBarLeading) { //error appear here
                    Button(action: {
                        model.modalCloseButtonTapped()
                    }) {
                        Image(systemName: "xmark")
                            .foregroundStyle(theme.color.content.primary)
                    }
                }
                
                ToolbarItem(placement: .topBarTrailing) {
                    Button(action: {
                        if model.showClearButton {
                            model.clearButtonTapped()
                        } else {
                            model.saveButtonTapped()
                        }
                    }) {
                        if model.showClearButton {
                            Text("Clear")
                                .foregroundStyle(theme.color.tag.rejected)
                        } else {
                            Image(systemName: "checkmark")
                                .foregroundColor(!model.statusInput.isEmpty ? theme.color.brand.primary : theme.color.content.tertiary)
                        }
                    }
                    .alert(isPresented: Binding.constant(model.state.showAlert)) {
                        Alert(
                            title: Text("✅"),
                            message: Text("Set Status")
                        )
                    }
                }
            }
            .navigationBarTitle("Set a Status", displayMode: .inline)
        }
    }
}

我正在尝试构建此功能,即显示在另一个页面上的简单文本输入。 一切都很好,直到我将所有属性从 ViewModel 分离到一个结构中。

当我移动这个属性时,错误开始了:model.state.showAlert

.alert(isPresented: Binding.constant(model.state.showAlert)) {
//
}
SwiftUI 视图 绑定 工具栏

评论


答:

0赞 Baffo rasta 11/15/2023 #1

我认为应该是这样的:

.toolbar {
    ToolbarItem(placement: .topBarLeading) {
        if model.showClearButton {
            Button {
                model.clearButtonTapped()
            } label: {
                Text("Clear")
                    .foregroundStyle(theme.color.tag.rejected)
            }
        } 
    }

    ToolbarItem(placement: .topBarLeading) {
        if !model.showClearButton() {
            Button {
                model.saveButtonTapped()
            } label: {
                Image(systemName: "checkmark")
                     .foregroundColor(!model.statusInput.isEmpty ? theme.color.brand.primary : theme.color.content.tertiary)
            }
        } 
    }
}
0赞 workingdog support Ukraine 11/17/2023 #2

假设是 类型,则尝试model.state.showAlertBool

.alert(isPresented: $model.state.showAlert) {...}

并将其移.toolbar{...}