Home > other >  Swift, Stuck on making an notes app, don't know what the problem is using text editor
Swift, Stuck on making an notes app, don't know what the problem is using text editor

Time:01-19

I am new to coding and am trying to make a notes app come to life, the issue is I have no idea how to make separate text editors in my for loops.

This is my code. When you run the project and create a green sticky note and type on it, it works fine, but if you do a second one of the same color, they have the same text. How do I fix this in a way that doesn't take hours of tedious work?

I have tried to use different ways to make a for loop. I have made one with normal lists and with a struct list that has different ids, both come up with the same text editor.

(Text editors don't copy over color, which makes me think it's the for loop because when I tried to use different variables for the bindings it didn't work either.)

//
//  ContentView.swift
//  Notesapp
//
//

import SwiftUI
import Foundation

struct newBoy: Identifiable {
    let id = UUID()
    let number: Int
}

class NewBoys: ObservableObject {
    @Published var numbs = [newBoy]()
}

struct ContentView: View {
    
    @StateObject var numbers1 = NewBoys()
    
    @State var buttonoff = true
    
    @State var recaddor: [GridItem] = [
        GridItem(.flexible()),
        GridItem(.flexible()),
        GridItem(.flexible()),
        GridItem(.flexible()),
    ]
    
    @State var glist = [Int]()
    @State var i = -1
    @State var gtext = "This be me"
    @State var blist = [Int]()
    @State var bman = -1000
    @State var btext = "Enter your notes here!"
    @State var bklist = [Int]()
    @State var bkman = -2000
    @State var bktext = "Enter your notes here!"
    @State var ylist = [Int]()
    @State var yman = -3000
    @State var ytext = "Enter your notes here!"
    @State var olist = [Int]()
    @State var oman = -4000
    @State var otext = "Enter your notes here!"
    @State var plist = [Int]()
    @State var pman = -5000
    @State var ptext = "Enter your notes here!"
    
    var body: some View {
        HStack{
            
            VStack {
                
                Text("Make new note!").padding().foregroundColor(Color.black)
                
                Button {
                    withAnimation(.easeIn) {
                        self.buttonoff.toggle()
                    }
                } label: {
                        Image(systemName: "plus")
                        .font(.title2)
                        .foregroundColor(.white)
                        .rotationEffect(.init(degrees: buttonoff ? 0 : 45))
                        .scaleEffect(buttonoff ? 1 : 1.3)
                        .padding()
                }.buttonStyle(PlainButtonStyle())
                    .background(Color.black)
                    .clipShape(Circle())
                    .padding()
                
                if buttonoff {
                }
                else {
                    Group {
                        Button {
                            recGreen()
                        } label: {
                            RoundedRectangle(cornerRadius: 25)
                                .fill(Color.green)
                                .frame(width: 30, height: 30)
                        }.buttonStyle(PlainButtonStyle())
                        Button {
                            recBlue()
                        } label: {
                            RoundedRectangle(cornerRadius: 25)
                                .fill(Color.blue)
                                .frame(width: 30, height: 30)
                        }.buttonStyle(PlainButtonStyle())
                        Button {
                            recBlack()
                        } label: {
                            RoundedRectangle(cornerRadius: 25)
                                .fill(Color.black)
                                .frame(width: 30, height: 30)
                        }.buttonStyle(PlainButtonStyle())
                        Button {
                            recYellow()
                        } label: {
                            RoundedRectangle(cornerRadius: 25)
                                .fill(Color.yellow)
                                .frame(width: 30, height: 30)
                        }.buttonStyle(PlainButtonStyle())
                        Button {
                            recOrange()
                        } label: {
                            RoundedRectangle(cornerRadius: 25)
                                .fill(Color.orange)
                                .frame(width: 30, height: 30)
                        }.buttonStyle(PlainButtonStyle())
                        Button {
                            recPurple()
                        } label: {
                            RoundedRectangle(cornerRadius: 25)
                                .fill(Color.purple)
                                .frame(width: 30, height: 30)
                        }.buttonStyle(PlainButtonStyle())
                    }.padding(20)
                        .scaleEffect(1.5)
                }
                
            }.frame(width: 100, height: 700, alignment: .top)
                .background(Color.white)
                .border(Color.gray, width: 2)
            VStack {
            
                ScrollView{
                    LazyVGrid(columns: recaddor){
                        
                        ForEach(numbers1.numbs, id: \.id) {o in
                            ZStack{
                            RoundedRectangle(cornerRadius: 25)
                                .fill(Color.green)
                                .frame(width: 250, height: 200)
                                .padding()
                                VStack{
                                    HStack{
                                        Spacer()
                                        Button {
                                        print("hi")
                                    } label: {
                                        Image(systemName: "minus")
                                    }
                                    .padding(.top, 25.0)
                                    .padding(.trailing, 30.0)
                                    .frame(alignment:.trailing)
                                }
                                    TextEditor(text: $gtext)
                                        .frame(width: 225, height: 150, alignment: .center)
                                        .cornerRadius(3.0)
                                        .colorMultiply(.green)
                                    Spacer()
                                }
                            }
                        }
                        
                        ForEach(blist, id: \.self) {blueman in
                            ZStack{
                            RoundedRectangle(cornerRadius: 25)
                                .fill(Color.blue)
                                .frame(width: 250, height: 200)
                                .padding()
                                VStack{
                                    HStack{
                                        Spacer()
                                        Button {
                                        blist = blist.filter({ Int in
                                            return Int != blueman
                                        })
                                    } label: {
                                        Image(systemName: "minus")
                                    }
                                    .padding(.top, 25.0)
                                    .padding(.trailing, 30.0)
                                    .frame(alignment:.trailing)
                                }
                                    TextEditor(text: $btext)
                                    Spacer()
                                }
                            }
                        }
                        ForEach(bklist, id: \.self) {bklueman in
                            ZStack{
                            RoundedRectangle(cornerRadius: 25)
                                .fill(Color.black)
                                .frame(width: 250, height: 200)
                                .padding()
                                VStack{
                                    HStack{
                                        Spacer()
                                        Button {
                                        bklist = bklist.filter({ Int in
                                            return Int != bklueman
                                        })
                                    } label: {
                                        Image(systemName: "minus")
                                    }
                                    .padding(.top, 25.0)
                                    .padding(.trailing, 30.0)
                                    .frame(alignment:.trailing)
                                }
                                    TextEditor(text: $bktext)
                                    Spacer()
                                }
                            }
                        }
                        ForEach(ylist, id: \.self) {ylueman in
                            ZStack{
                            RoundedRectangle(cornerRadius: 25)
                                .fill(Color.yellow)
                                .frame(width: 250, height: 200)
                                .padding()
                                VStack{
                                    HStack{
                                        Spacer()
                                        Button {
                                        ylist = ylist.filter({ Int in
                                            return Int != ylueman
                                        })
                                    } label: {
                                        Image(systemName: "minus")
                                    }
                                    .padding(.top, 25.0)
                                    .padding(.trailing, 30.0)
                                    .frame(alignment:.trailing)
                                }
                                    TextEditor(text: $ytext)
                                    Spacer()
                                }
                            }
                        }
                        ForEach(olist, id: \.self) {olueman in
                            ZStack{
                            RoundedRectangle(cornerRadius: 25)
                                .fill(Color.orange)
                                .frame(width: 250, height: 200)
                                .padding()
                                VStack{
                                    HStack{
                                        Spacer()
                                        Button {
                                        olist = olist.filter({ Int in
                                            return Int != olueman
                                        })
                                    } label: {
                                        Image(systemName: "minus")
                                    }
                                    .padding(.top, 25.0)
                                    .padding(.trailing, 30.0)
                                    .frame(alignment:.trailing)
                                }
                                    TextEditor(text: $otext)
                                    Spacer()
                                }
                            }
                        }
                        ForEach(plist, id: \.self) {plueman in
                            ZStack{
                            RoundedRectangle(cornerRadius: 25)
                                .fill(Color.purple)
                                .frame(width: 250, height: 200)
                                .padding()
                                VStack{
                                    HStack{
                                        Spacer()
                                        Button {
                                        plist = plist.filter({ Int in
                                            return Int != plueman
                                        })
                                    } label: {
                                        Image(systemName: "minus")
                                    }
                                    .padding(.top, 25.0)
                                    .padding(.trailing, 30.0)
                                    .frame(alignment:.trailing)
                                }
                                    TextEditor(text: $ptext)
                                    Spacer()
                                }
                            }
                        }
                        
                    }
                }
                
            }.frame(width: 1100)
        }
    }
    func recGreen() {
        let i = newBoy(number: 0)
        numbers1.numbs.append(i)
        print(numbers1.numbs)
    }
    func recBlue() {
        bman  = 1
        blist.append(bman)
    }
    func recBlack() {
        bkman  = 1
        bklist.append(bkman)
    }
    func recYellow() {
        yman  = 1
        ylist.append(yman)
    }
    func recOrange() {
        oman  = 1
        olist.append(oman)
    }
    func recPurple() {
        pman  = 1
        plist.append(pman)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

CodePudding user response:

It's because you are using the same binding.

In your ForEach, you are creating multiple TextEditors but they are editing the same variable like $otext. You need to create a struct that holds the text, and the array you use in the ForEach should be of type YourStruct. Then you pass the text to TextEditors.

struct Note: Identifiable {
    var id: Int //your current array type, i.e if your array is [Int] use Int.
    var text: String //this is the text to pass to the editors
}
  •  Tags:  
  • Related