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