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

问题描述 投票:0回答:2

静态方法“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 view binding toolbar
2个回答
0
投票

我觉得应该是这样的:

.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
投票

假设

model.state.showAlert
是类型
Bool
,那么尝试

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

并将其移至外部

.toolbar{...}

© www.soinside.com 2019 - 2024. All rights reserved.