0

So I am trying to pass a variable from one view to another using Bindings. When I tap on the ExpenseRow, it sets the index to the expenseArrayIndex State variable and prints the proper index. However when I print the variable in the view I am passing it to, it shows up as the default value of -1 (or if I initialize it as an Int? it is nil in the other view).

Also if I have multiple object in my m_expensesArray, once I click on the second object, all the indices will be transferred to the second view there on out fine. So I'm fairly certain it is something with the initialization but not sure exactly what and I am all out of ideas.

My Main ExpenseView struct where I set the expenseArrayIndex = to the index of the expense object I tapped on

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)
            }
        }
    }
}

My EditExpenseView that I am trying to pass the expenseArrayIndex into. Here is where the value comes out as -1 or 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)
    }

I am still fairly new to SwiftUI and new to posting on stack overflow so sorry if It isn't formatted entirely right

epartridge
  • 31
  • 3
  • I am not sure anyone is going to follow this. there are way too many things not shown. Please see [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and post all the code necessary to run. – Yrb Jan 15 '21 at 23:54
  • 1
    Most of your problems are because of the switch statement within .fullScreenCover. Try using one of the methods in [my answer here](https://stackoverflow.com/questions/65416673/multiple-bottom-sheets-the-content-doesnt-load-swiftui/65416999#65416999) on how to handle multiple .sheets within a View, which work the same as .fullScreenCover. – nicksarno Jan 16 '21 at 03:23
  • @nicksarno Thank you. That makes sense. I change my code using your second example of different views for each button. This sort of works now as I am now running into issues of multiple sheets being displayed within a ForEach loop. Have you encountered this issue? – epartridge Jan 18 '21 at 00:03
  • I believe you can use the 1st method on that link to handle a ForEach loop. The trick is to have 1 View within the .sheet destination and to make the variable that changes be Binding to that next View. – nicksarno Jan 18 '21 at 03:50
  • I'll note that it will be easier (and probably more efficient) to use NavigationLink for the ForEach loop's segue, if you are ok with changing the UI a little. – nicksarno Jan 18 '21 at 03:53

0 Answers0