我想绑定2个swiftui视图时遇到问题。一个显示发票的创建,另一个显示发票的项目。创建发票后,将使用api上传该发票以访问一个负责的系统,因此,我使用与该负责的系统进行通信的JSON创建了该发票(发票),因此创建了发票项目。当我要在发票的创建和项目的详细信息之间添加NavigationLink时失败。 这是我的代码
这是发票项目的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
}
}
这是我创建发票的地方:
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()
}
}
这是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()
}
}
}
这是我添加视图以编辑发票项目的地方:
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()
}
}
在此列表中添加NavigationLink时,我会遇到各种错误
如果有人可以给我一些解决方法的建议,我将不胜感激。 我尝试遵循apple的流程,但是我不知道如何添加(以及在其中添加)环境对象。
谢谢
答案 0 :(得分:1)
使用显式参数定义代替
List {
ForEach(itemsFactura) { item in
NavigationLink(destination: AddingItemFactura()) { // another context
RowInvoiceItem(invoiceItem: item) // must be passed explicitly
}
}.onDelete(perform: removeRows)
}