@State 变量在通过绑定变量 SwiftUI 时不更新

时间:2021-01-15 21:36:44

标签: binding swiftui state

所以我试图使用绑定将变量从一个视图传递到另一个视图。当我点击 ExpenseRow 时,它将索引设置为费用数组索引状态变量并打印正确的索引。但是,当我在传递给它的视图中打印变量时,它显示为默认值 -1(或者如果我将其初始化为 Int?它在另一个视图中为零)。

此外,如果我的 m_expensesArray 中有多个对象,一旦我单击第二个对象,所有索引都将转移到第二个视图中。所以我很确定这与初始化有关,但不确定到底是什么,我完全没有想法。

我的主要费用视图结构,我将费用数组索引设置为我点击的费用对象的索引

struct ExpenseView: View {
    
    @ObservedObject var gloablBudgetData: BudgetData = BudgetData.shared
    
    @State var isModalA: Bool = false
    @State var isModalE: Bool = false
    @State var expenseArrayIndex: Int = -1
    
    @State var activeSheet: ActiveSheet? = nil
    
    var body: some View {
        
        VStack (spacing: 0)
        {
            Text("ADD EXPENSE")
                .font(.title3)
                .fontWeight(.bold)
                .foregroundColor(Color.white)
                .multilineTextAlignment(.center)
                .padding(5.0)
                .background(darkGreen)
                .onTapGesture(count: 1, perform: {
                    activeSheet = .addExpense
                })
            
            Spacer()
            
            List
            {
                Section(header: ListHeader())
                {
                    ForEach(gloablBudgetData.m_expensesArray.indices, id: \.self) { i in
                        VStack(spacing: 0){
                            ExpenseRow(expense: gloablBudgetData.m_expensesArray[i])
                                .padding(.all, 5.0)
                                .onTapGesture(count: 1, perform: {
                                    print("Expense Index: " + String(i))
                                    expenseArrayIndex = i
                                    activeSheet = .editExpense
                                    print("Var index: ", expenseArrayIndex)
                                })
                            
                            Divider()
                                .frame(height: 1)
                                .background(Color.black)
                        }
                        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
                        .listRowInsets(EdgeInsets())
                        .background(Color.white)
                    }
                }
            }
            .frame(width: 300, height: 450, alignment: .center)
            .cornerRadius(30)
            .shadow(radius: 50)
        }
        .fullScreenCover(item: $activeSheet) { item in
            switch item {
            case .addExpense:
                AddExpenseView(activeSheet: $activeSheet)
            case .editExpense:
                EditExpenseView(activeSheet: $activeSheet, expenseArrayIndex: $expenseArrayIndex)
            }
        }
    }
}

我试图将费用数组索引传递到我的 EditExpenseView。这里的值是 -1 或 nil

struct EditExpenseView: View {
    
    @State private var expenseName: String = ""
    @State private var expenseDate: String = ""
    @State private var expenseCategory: String = ""
    @State private var expenseAmount: String = ""
    
    @State private var validExpenseName: Bool = true
    @State private var validExpenseDate: Bool = true
    @State private var validExpenseCategory: Bool = true
    @State private var validExpenseAmount: Bool = true
    
    @ObservedObject var gloablBudgetData: BudgetData = BudgetData.shared
    
    @Binding var m_activeSheet: ActiveSheet?
    @Binding var m_expenseArrayIndex: Int
    
    init(activeSheet: Binding<ActiveSheet?>, expenseArrayIndex: Binding<Int>)
    {
        self._m_activeSheet = activeSheet
        self._m_expenseArrayIndex = expenseArrayIndex
        
        print("Index: ", m_expenseArrayIndex)
    }

我对 SwiftUI 还是比较陌生,对堆栈溢出问题的发布也是新手,如果格式不完全正确,我很抱歉

0 个答案:

没有答案