SwiftUI将参数传递到View并不总是可用

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

我在调用时将参数传递给SwiftUI中的View时遇到问题。我有这个视图

import SwiftUI

struct GoodsItemFilterView: View {
    @Environment(\.presentationMode) var presentationMode

    @State var ref1Array: [String] = []
    @State var ref2Array: [String] = []
    @State var ref3Array: [String] = []

    @State var stockStatusArray: [String] = []
    @State var zoneArray: [String] = []

    @State var selectorRef1 = 0
    @State var selectorRef2 = 0
    @State var selectorRef3 = 0
    @State var selectorStockStatus = 0
    @State var selectorZone = 0

    var body: some View {
        NavigationView {
            Form{
                Section(header: Text("Zone"), content: {
                    Picker(selection: $selectorZone, label:
                    Text("Zone")) {
                        ForEach(0 ..< zoneArray.count, id:\.self) {
                            Text(self.zoneArray[$0])
                        }
                    }
                })

                Section(header: Text("References"), content: {
                    Picker(selection: $selectorRef1, label:
                    Text("Reference 1")) {
                        ForEach(0 ..< ref1Array.count, id:\.self) {
                            Text(self.ref1Array[$0])
                        }
                    }

                    Picker(selection: $selectorRef2, label:
                    Text("Reference 2")) {
                        ForEach(0 ..< ref2Array.count, id:\.self) {
                            Text(self.ref2Array[$0])
                        }
                    }

                    Picker(selection: $selectorRef3, label:
                    Text("Reference 3")) {
                        ForEach(0 ..< ref3Array.count, id:\.self) {
                            Text(self.ref3Array[$0])
                        }
                    }
                })

                Section(header: Text("Status"), content: {
                    Picker(selection: $selectorStockStatus, label:
                    Text("Condition")) {
                        ForEach(0 ..< stockStatusArray.count, id:\.self) {
                            Text(self.stockStatusArray[$0])
                        }
                    }
                })

                Button(action: {
                    self.selectorZone = 0
                    self.selectorRef1 = 0
                    self.selectorRef2 = 0
                    self.selectorRef3 = 0
                    self.selectorStockStatus = 0
                }, label: {
                    HStack(){
                        Spacer()
                        Image(systemName: "return")
                        Text("Reset filters")
                        Spacer()
                    }

                })

            }.navigationBarTitle("Filter")
                .navigationBarItems(leading: (
                    Button(action: {
                        self.presentationMode.wrappedValue.dismiss()
                    }, label: {
                        Text("Cancel")
                    }
                    )
                    ), trailing: (
                        Button(action: {

                            self.presentationMode.wrappedValue.dismiss()
                        }, label: {
                            Text("Done")
                        }
                        )
                ))
        }.onAppear{
            self.ref1Array.insert("***ALL***", at: 0)
            self.ref2Array.insert("***ALL***", at: 0)
            self.ref3Array.insert("***ALL***", at: 0)

            self.stockStatusArray.insert("***ALL***", at: 0)
            self.zoneArray.insert("***ALL***", at: 0)


        }
    }
}

struct GoodsItemFilter_Previews: PreviewProvider {
    static var previews: some View {
        GoodsItemFilterView(ref1Array: ["MAX100", "MAX101", "MAX102"], ref2Array: ["REF2_100", "REF2_101"], ref3Array: ["REF3_100", "REF3_101"])
    }
}

并且当我调用它时,我可以将数组的值作为参数传递:

GoodsItemFilterView(ref1Array: ["MAX100", "MAX101", "MAX102"], ref2Array: ["REF2_100", "REF2_101"], ref3Array: ["REF3_100", "REF3_101"])  

现在我有另一种视图,基本上是该视图的副本,并具有一些更改的名称,等等

//
//  OrderHeaderFilter.swift
//  WMS Toolbox
//
//  Created by Max on 2020-01-24.
//  Copyright © 2020 Max. All rights reserved.
//

import SwiftUI
//import Combine

struct OrderHeaderFilterView: View {
    @Environment(\.presentationMode) var presentationMode

    @State var orderTypeArray: [String] = []
    @State var carrierArray: [String] = []

    @State var fromStatus2 = UserDefaults.standard.string(forKey: "view.orderHeaderFilter.fromStatus")
    //  @State private var fromStatus2 = "040"

    @State private var direction = ""
    @State private var fromStatus = ""
    @State private var toStatus = ""
    @State private var orderType = ""


    @State var selectorOrderType = 0
    @State var selectorCarrier = 0

    @State private var selectorIndex = 1
    @State private var fromStatusSelectorIndex = 6
    @State private var toStatusSelectorIndex = 2

    @State private var directions = ["Inbound","Outbound","Both"]

    private var orderStatusFromArray: [String] = ["005", "010", "022", "025", "030", "035", "040", "045", "046", "047", "060"]
    private var orderStatusToArray: [String] = ["005", "010", "022", "025", "030", "035", "040", "045", "046", "047", "060"]

    @State var orderStatus = OrderStatus.s05

    enum OrderStatus: String, CaseIterable, Identifiable {
        case s05 = "005"
        case s10 = "010"
        case s22 = "022"
        case s25 = "025"
        case s30 = "030"
        case s35 = "035"
        case s40 = "040"
        case s45 = "045"
        case s46 = "046"
        case s60 = "060"

        var id: String { rawValue }
    }




    enum Direction: String, CaseIterable{
        case outbound = "1"
        case inbound = "2"
        case both = "3"

        init(type: String) {
            switch type {
            case "1": self = .outbound
            case "2": self = .inbound
            case "3": self = .both
            default: self = .both
            }
        }

        var text: String {
            switch self {
            case .outbound: return "Outbound"
            case .inbound: return "Inbound"
            case .both: return "Both"
            }
        }
    }

    init(){
        //nothing here
    }

    var body: some View {

        return NavigationView{
            Form{
                HStack{
                    Text("Direction")
                    Spacer()
                    Picker(selection: $direction, label:
                    Text("Direction")) {
                        ForEach(directions, id:\.self) {
                            status in
                            Text(status)
                        }
                    }
                    .pickerStyle(SegmentedPickerStyle())
                }

                Picker(selection: $fromStatus, label:
                Text("From Status")) {
                    ForEach(orderStatusFromArray, id:\.self) {
                        status in
                        Text(status)
                    }
                }

                Picker(selection: $toStatus, label:
                Text("To Status")) {
                    ForEach(orderStatusFromArray, id:\.self) {
                        status in
                        Text(status)
                    }
                }

            }.navigationBarTitle("Filter")
                .navigationBarItems(leading: (
                    Button(action: {
                        self.presentationMode.wrappedValue.dismiss()
                    }, label: {
                        Text("Cancel")
                    }
                    )
                    ), trailing: (
                        Button(action: {


                            self.presentationMode.wrappedValue.dismiss()
                        }, label: {
                            Text("Done")
                        }
                        )
                ))

        }.onAppear{

            self.direction = UserDefaults.standard.string(forKey: "view.orderHeaderFilter.direction")!
            self.fromStatus = UserDefaults.standard.string(forKey: "view.orderHeaderFilter.fromStatus")!
            self.toStatus = UserDefaults.standard.string(forKey: "view.orderHeaderFilter.toStatus")!

            self.orderTypeArray.insert("***ALL***", at: 0)
            self.carrierArray.insert("***ALL***", at: 0)



        }
    }
}

struct OrderHeaderFilter_Previews: PreviewProvider {
    static var previews: some View {
        OrderHeaderFilterView()
    }
}

并且当我调用它时,它并没有提示我将数组作为参数传递:

OrderHeaderFilterView()  

[这两个视图要求初始化有关的论点,而另一个不要求初始化的观点之间有什么区别?为了清楚起见,最后我想传递参数,所以GoodsItemFilterView()正是在做我需要的事情。

view arguments swiftui
2个回答
0
投票

EnvironmentObjects不通过init方法传递,而是被隐式注入。 orderTypeArray和carrierArray已经具有初始值。因此,OrderHeaderFilterView()不会提示您输入参数。


0
投票

发现问题:

我在代码中有这部分

init(){
        //nothing here
    }

需要删除,否则它将不要求任何变量。

另一个问题是我不了解的问题:

private var orderStatusFromArray: [String] = ["005", "010", "022", "025", "030", "035", "040", "045", "046", "047", "060"]
    private var orderStatusToArray: [String] = ["005", "010", "022", "025", "030", "035", "040", "045", "046", "047", "060"]

如果我将var更改为let,它将按预期工作。另一种选择是在一开始就删除私有文件。这样看来,只要您有

private var ...

在您的代码中,所有参数都变为私有。也许我在这里错过了一些东西,但对我来说似乎是个bug。

热门问题
推荐问题
最新问题