[英]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.