0

I am trying to get data from one view to another. I can not figure out how to get values from the fourth view array into the Third view.
I am not using storyboards. I tried using @EnvironmentObject but can not make it work. New to coding. In xcode I am using watchos without app.

I tried to strip out most of the code and leave just the important stuff that can be tested. I used NavigationLink(destination: )to transfer between views.

enter code here
class viewFromEveryWhere: ObservableObject {
@Published var testname2: String = "testTTname"

}

struct secondView: View {
   var body: some View {
       Text("second view")
       List(1..<7) {
           Text("\($0)")
       }
   }
}
 struct thirdView: View {
    @EnvironmentObject var testname2: viewFromEveryWhere
    @EnvironmentObject var testSixTestArray: viewFromEveryWhere
    @State var sixTestArray:[String] = ["ww","GS","DW","CD","TS","JW",]

    var body: some View {

        List(sixTestArray, id:\.self) {
            Text($0)
         }
    }
}
struct fourthView: View {
    @StateObject var testname2 = viewFromEveryWhere()
   @State private var name: String = ""
   @State var testSixTestArray:[String] = []
    func collectName () {
        print("collectName triggered")
        if testSixTestArray.count < 5 {
         //   testSixTestArray.append(name)
        print(name)
        print(testSixTestArray)
       
        }
       // .enviromentObject(testSixTestArray)
    }


var body: some View {
    VStack(alignment: . leading) {
        Text("Type a name")
        TextField("Enter your name", text: $name)
        Text("Click to add, \(name)!")
       // Button("click this if \(name) is correct") {}
        Button(action:{
             print("Button Tapped")
           collectName()
            print(testSixTestArray.count)
                name = ""
            
        }) {
            
            Text("Add \(name) to list")

        }
       // .buttonStyle(GrowingButton1())
        
    }
    
    Text("forth view")
   // testSixTestArray.append(name)
        .environmentObject(testname2)
    }

}

/*func presentTextInputControllerWithSuggestions(forLanguage suggestionsHandler: 
((String)-> [Any]?)?,
                                           allowedInputMode inputMode: 
WKTextInputMode,
                                           completion: @escaping ([Any]?) -> Void) {}

 */

struct ContentView: View {

@State var sixNameArray:[String] = ["","","","","","",]

@State var messageTextBox: String = "Start"
@State var button1: String = "Button 1"
@State var button2: String = "Button 2"
@State var button3: String = "Button 3"


var body: some View {
    NavigationView {
  VStack{
        Text(messageTextBox)
                .frame(width: 120, height: 15, alignment: .center)
                .truncationMode(.tail)
                .padding()
        NavigationLink(destination: secondView(),
                       label:{
                        Text(button1)
                       })
            .navigationBarTitle("Main Page")
        NavigationLink(destination: thirdView(),
                       label:{
                        Text(button2)
                       })
        NavigationLink(destination: fourthView(),
                       label:{
                        Text(button3)
                       })
          }
        }
}

}

enter code here
Dmar
  • 25
  • 6
  • When I add new strings to the forth view or array in forth view. I am not able to access it in the third view. I might have deleted a bit too much code before adding my questionI am not sure if I am on the wrong track with @enviroment. – Dmar Dec 26 '21 at 11:54
  • You can only have one environment object of each type. You are expecting 2 in view 3 that are the same type – lorem ipsum Dec 26 '21 at 14:22
  • I don't know enough to know what that means. – Dmar Dec 27 '21 at 02:40

0 Answers0