Post

Replies

Boosts

Views

Activity

How to use the same array in different views and save it with app storage?
I'm trying to get the array listaDeJugadores to be used in all my views. I am able to add a Player to it and its name and age. However, when I try to display it in the view called otherPage, it gives me an error, thread 1: Fatal error: Index out of range. I also want to be able to save the array listaDeJugadores with app Storage, however, I don't know if it's possible to use app Storage in this case. Any help will be appreciated! Thank you!. import SwiftUI class Todo: ObservableObject {   class Player {           var name = ""           var age = 0   }   @Published var listaDeJugadores: [Player] = [] } struct ContentView: View {          class Player {           var name = ""           var age = 0   }          @State var index = 0       @StateObject var lista = Todo()       @State var playerName = ""           @State var firstView = true   @State var secondView = false   @State var agePlayer = 0       var body: some View {           if firstView == true {       VStack {                   ZStack{           Color.pink                       TextField("Player name", text: $playerName)         }         .frame(width: 350, height: 50)         .cornerRadius(25)                   ZStack{           Color.pink           TextField("Player Age", value: $agePlayer, format: .number)         }         .frame(width: 350, height: 50)         .cornerRadius(25)                   Button(action: {                       lista.listaDeJugadores.append(Todo.Player())           lista.listaDeJugadores[index].name = playerName           lista.listaDeJugadores[index].age = agePlayer           playerName = ""           index += 1         }, label: {                       ZStack{             Color.black                           Text("Add")               .font(.title)               .foregroundColor(Color.white)                         }           .frame(width: 350, height: 50)           .cornerRadius(25)                     })         Button(action: {           firstView = false           secondView = true         }, label: {           ZStack{             Color.black                           Text("Done")               .font(.title)               .foregroundColor(Color.white)           }           .frame(width: 350, height: 50)           .cornerRadius(25)         })       }     } else if secondView == true {               otherPage()         .environmentObject(Todo())     }        }     } struct otherPage: View {       @EnvironmentObject var lista: Todo               var body: some View {     Text("\(lista.listaDeJugadores[0].name)")   } }
0
0
1.4k
Dec ’22
How to modify attributes of class inside ForEach?
I'm trying to change the attribute of points by adding 1 every time the button is pressed, however, it does not change. How could I accomplish this?           class Person{           var name = ""     var points = 0   }   var Greg = Person()   var Jeo = Person()   var Humio = Person()       init(Greg: Person = Person(), Jeo: Person = Person(), Humio: Person = Person()) {     self.Greg = Greg     self.Jeo = Jeo     self.Humio = Humio           Greg.name = "Greg"     Jeo.name = "Jeo"     Humio.name = "Humio"   }           var body: some View {           var nombres:[Person] = [Greg, Jeo, Humio]          VStack {       ForEach(nombres, id: \.name){         names in                   ZStack{           Rectangle()            .frame(width: 200, height: 200)           VStack{             Text("\(names.points)")               .foregroundColor(Color.white)                           Button(action: {                               names.points += 1                             }, label: {                               Text(names.name)                             })                         }                     }                 }     }     .padding()   } }
1
0
397
Dec ’22
How to create an instance of my class?
I'm trying to create a new instance of my class "people" and add it to my list called listOfPeople. At the start, I ask how many people am I going to add. After I answer that, I add 1 to the variable "level" so I can continue as if it where a for loop. Now at level 1, I add the name and numberAssigned to the new instance which would supposedly go in listOfPeople in the position in which the variable counter is on. After I click the button with text "Add name" I would add another name and so on until I reach the numberOfpeople, so again, trying to simulate a for loop (I'm aware I have not added the logic for the textfield and button to add a new instance and stop when counter reaches the numberOfpeople). However, in this case, after I add the persons name and numberAssigned, it gives me the error in image number three. Which says "Thread 1: Fatal error: Index out of range." How could I make this work? I learned how to do this in c++ but I clearly do not know how to accomplish it in swiftui. Any help is appreciated! ;) I could make it work this way, the only problem is that if the user wants to add 1,000 people, I would have to have 1,000 variables pre-created, which I do not want, I want to be able to do it as a for-loop per se.
1
0
414
Dec ’22
Making a new View with same functionality as other views but with different content
I have a tasks/notes app in the form of a list that when I add a task, I give it a title, for example “Groceries". I want to be able to click “groceries" and for there to appear a new list with new notes that would be such as beverages or food. Such notes would be added by the user.  Then in the main section if I want to add a new task or notes such as “Chores” ,when I click on chores it takes me to a new page in which the user can add chores.  However, right now I only know how to take it to the same view. So in other words, if i click on “groceries", I will see the same as if I click on “Chores”. In my app, it takes me to exampleView as you can see below. My app is made around core data, what I essentially want is for when I click a note such as groceries or chores, I can have a page in which I store new notes.  How can I make it so when I click on a note on the main page, it takes me to a new page? Any advice, tutorials, or anything helps. Thank You. ForEach(notesVM.notes, id: \.id){note in          NavigationLink(destination: exampleView()){          NotesCell(note: note)         } This is what I Have: This is what I Want:
1
0
945
Sep ’22