简体   繁体   English

添加 NavigationLink 时出现 Swiftui 问题

[英]Swiftui issue when I add a NavigationLink

I have an issue when I want to bind 2 swiftui views.我想绑定 2 个 swiftui 视图时遇到问题。 Ones displays the creation of an invoice the other one displays the invoice items.一个显示发票的创建,另一个显示发票项目。 After the invoice is created it is uploaded using an api to access a accountable system, therefore I create it (the invoice) using the JSON I use to communicate with the accountable system and so the invoice items.创建发票后,它使用 api 上传以访问会计系统,因此我使用 JSON 创建它(发票),用于与会计系统通信,因此发票项目。 I fail when I want to add a NavigationLink between the creation of the invoice and the detailed of the item.当我想在发票的创建和项目的详细信息之间添加 NavigationLink 时,我失败了。 This is my code这是我的代码

This is the json representation of the invoice item这是发票项目的 json 表示

    // MARK: - ItemsFactura
struct ItemFactura: Codable {
    var descripcion, unidadMedida, ccosto1, ccosto2: String?
    var cantidad, importeUnitario, iva: Double?
    var idPlanCuenta, codigo, almacen: String?

    enum CodingKeys: String, CodingKey {
        case descripcion = "Descripcion"
        case unidadMedida, ccosto1, ccosto2
        case cantidad = "Cantidad"
        case importeUnitario = "ImporteUnitario"
        case iva = "IVA"
        case idPlanCuenta, codigo, almacen
    }
}

This is where I create my invoice:这是我创建发票的地方:

    final class DummyItemFactura:ObservableObject, Identifiable {
    @Published var id:Int?
    @Published var item:ColppyApiRequests.ItemFactura?
}

struct CreateInvoice: View {

    @State private var showingInvoiceNumber = false
    @State private var showingProveedores = false
    @State private var showingItemsFactura = false
    @State private var isHacienda = false
    @State private var isMercado = false

    @State private var fechaFactura = Date()
    @State private var fechaContable = Date()
    private var tipoDeFactura = ["A", "B", "C", "M", "F", "E"]
    @State private var seleccionTipoDeFactura = ""
    @State private var nroDeFactura = ""
    @State var proveedorSeleccionado:ColppyApiResponses.ListarProveedorResponseData?
    @State var numeroDeFactura:String?
    @State private var fechaDeCompra = Date()
    var lugaresDeCompra = ["Mercado", "Remate Feria", "Directo"]
    @State var itemsFactura = [DummyItemFactura]()
    @State private var lugarDeCompra = 0

    func removeRows(at offsets: IndexSet) {
        itemsFactura.remove(atOffsets: offsets)
    }

    var  body: some View {
        Form {
            Group {
                Button("Seleccionar proveedor"){
                    self.showingProveedores.toggle()}
                    .sheet(isPresented: $showingProveedores) {
                        SelectProveedor(isPresented: self.$showingProveedores,
                                        proveedorSeleccionado: self.$proveedorSeleccionado)}
                Text(self.proveedorSeleccionado?.nombreFantasia ?? "Proveedor seleccionado")
                    .font(.system(size: 15, weight: .light, design: .default))
                Toggle("Hacienda", isOn: $isHacienda)
                Picker(selection: $lugarDeCompra, label:Text("Lugar de compra")) {
                    ForEach(0..<self.lugaresDeCompra.count) {
                        Text(self.lugaresDeCompra[$0]).tag($0)
                    }
                }.pickerStyle(SegmentedPickerStyle())
                    .disabled(!isHacienda)
                DatePicker(selection:$fechaDeCompra, displayedComponents: .date) {
                    Text("Fecha de compra")
                }.disabled(!isHacienda)
                HStack {
                    Text("Items factura")
                    Spacer()
                    Image(systemName: "plus.circle")
                        .onTapGesture {
                            print("add item to the array")
                            self.itemsFactura.append(DummyItemFactura())
                    }.padding(10)
                }
                List {
                    ForEach(itemsFactura) {
                            RowInvoiceItem(invoiceItem: $0)
                    }.onDelete(perform: removeRows)
                }
                HStack {
                    Text("Importe Gravado")
                    Spacer()
                    Text("$22.448,58")
                }
                HStack {
                    Text("Importe No Gravado")
                    Spacer()
                    Text("$342,27")
                }
            }
            Group {

                DatePicker(selection: $fechaFactura, displayedComponents: .date) {
                    Text("Fecha de la factura")
                }
                DatePicker(selection: $fechaContable, displayedComponents: .date) {
                    Text("Fecha contable")
                }
                Picker(selection: $seleccionTipoDeFactura, label: Text("Tipo de Factura")){
                    ForEach(0..<self.tipoDeFactura.count) {
                        Text(self.tipoDeFactura[$0]).tag($0)
                    }
                }
                Button("Ingresar numero de factura") {
                    self.showingInvoiceNumber.toggle()
                }
                .sheet(isPresented: $showingInvoiceNumber) {
                    InvoiceNumberView(isPresented: self.$showingInvoiceNumber, numeroDeFactura:self.$numeroDeFactura)
                }
                Text(numeroDeFactura ?? "Numero de factura")
            }
        }
    }
}

struct CreateInvoice_Previews: PreviewProvider {
    static var previews: some View {
        CreateInvoice()
    }
}

This is the RowInvoiceItemView这是 RowInvoiceItemView

struct RowInvoiceItem: View {

    var invoiceItem: DummyItemFactura

    var body: some View {
        VStack() {
            Text(invoiceItem.item?.descripcion ?? "No item" )
                .font(.system(size: 14, weight: .semibold,
                design: .monospaced)).padding(10)
            Spacer()
            HStack {
                HStack {
                    Text("Cant: \(invoiceItem.item?.cantidad ?? 0, specifier: "%.2f")")
                    Text(invoiceItem.item?.unidadMedida ?? "No item")
                }
                Spacer()
                Text("ImpUnit: \(invoiceItem.item?.importeUnitario ?? 0, specifier: "%.2f")")
                Spacer()
                HStack {
                    Text("IVA: \(invoiceItem.item?.iva ?? 0, specifier: "%.2f")")
                    Text("%")
                }
                Spacer()
            }.font(.system(size: 10, weight: .light, design: .monospaced))
            Spacer()
        }
    }
}

This is where I add the view to edit the invoice item:这是我添加视图以编辑发票项目的地方:

struct AddingItemFactura: View {


    var itemFactura:ColppyApiRequests.ItemFactura?
    @State private var showingSeleccionarCuentaContable = false
    @State private var descripcion = ""
    @State private var unidadDeMedida = ""
    @State private var cantidad = ""
    @State private var precioPorUnidadDeMedida = ""
    @State private var IVA = 0
    @State private var cuentaContable:ColppyApiResponses.ListarCuentasContablesResponse.CuentaContable?
    @State private var subtotal = 0.0000
    private var ivas = ["0", "2.5", "5", "10.5", "17.1", "21", "27"]


    var body: some View {
        Form {
            TextField(itemFactura?.descripcion ?? "Descripcion", text: $descripcion)
                .keyboardType(.decimalPad)
            TextField("Unidad de medida", text:$unidadDeMedida)
            TextField("Precio por unidad de medida", text:$precioPorUnidadDeMedida)
                .keyboardType(.decimalPad)
            Text("Alicuota de IVA")
            Picker(selection: $IVA, label:Text("Alicuota del IVA")) {
                ForEach(0..<self.ivas.count) {
                    Text(self.ivas[$0]).tag($0)
                }
            }.pickerStyle(SegmentedPickerStyle())
            Button("Seleccionar Cuenta Contable") { self.showingSeleccionarCuentaContable.toggle() }
                .sheet(isPresented: self.$showingSeleccionarCuentaContable){
                    SelectCuentaContable(isPresented: self.$showingSeleccionarCuentaContable, cuentaContableSeleccionada: self.$cuentaContable)
            }
            Text(cuentaContable?.Descripcion ?? "Cuenta contable")
            Text("Subtotal : \(subtotal)")
            //Text("IVA: \()")
        }
    }
}

struct AddingItemFactura_Previews: PreviewProvider {

    static var previews: some View {
        AddingItemFactura()
    }
}

I get all sort of errors when I add the NavigationLink in this List在此列表中添加 NavigationLink 时出现各种错误

在此处输入图像描述

If anyone can give me a hit of how to solve this I would appreciate.如果有人能告诉我如何解决这个问题,我将不胜感激。 I try to follow apple's flow but I don't know how to add (and where) the environment object.我尝试遵循苹果的流程,但我不知道如何(以及在哪里)添加环境 object。

Thanks谢谢

Use instead explicit argument definition改用显式参数定义

List {
    ForEach(itemsFactura) { item in
       NavigationLink(destination: AddingItemFactura()) { // another context
            RowInvoiceItem(invoiceItem: item) // must be passed explicitly
       }
    }.onDelete(perform: removeRows)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在 SwiftUI 中重新访问之前单击的 NavigationLink 时,NavigationLink 冻结 - NavigationLink freezes when trying to revisit previously clicked NavigationLink in SwiftUI 如何使用 SwiftUI 将上下文菜单添加到 NavigationLink 中? - How to add Context Menu into NavigationLink using SwiftUI? SwiftUI - 单击 NavigationLink 时触发其他操作 - SwiftUI - Trigger other actions when click NavigationLink 在 SwiftUI 中呈现 NavigationLink 时,过渡 animation 消失了 - Transition animation gone when presenting a NavigationLink in SwiftUI 在 SwiftUI 中使用 NavigationLink 导航时如何隐藏 TabBar? - How to hide the TabBar when navigate with NavigationLink in SwiftUI? SwiftUI,使用 actionSheet 时出现奇怪的 NavigationLink 行为 - SwiftUI, weird NavigationLink behavior when working with actionSheet SwiftUI Picker 在通过 NavigationLink 显示时崩溃 - SwiftUI Picker crashes when displayed via NavigationLink SwiftUI - 显示 contextMenu 时,NavigationLink 的内容不可读 - SwiftUI - NavigationLink' content is not readable when contextMenu is presented SwiftUI 如果我将 NavigationLink 和 a.contextMenu 添加到列表中,则不显示列表选择。 这是一个已知的错误? - SwiftUI List selection doesn’t show If I add a NavigationLink and a .contextMenu to the list. Is this a known bug? 如何使用 Button Tap 移动 SwiftUI 中的视图或向 NavigationLink 添加操作? - How to move Views in SwiftUI with Button Tap or add an action to NavigationLink?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM