Showing Multiple Instances of View and Showing Results from Them

I have the following lines of code to show multiple instances of View (MyTextView)

import SwiftUI

struct ContentView: View {
	@State var myTextViews = [MyTextView]()
	
	var body: some View {
		VStack {
			Button {
				
			} label: {
				Text("Show me your current text strings")
			}.padding(.vertical, 10.0)
			
			VStack {
				ForEach(0 ..< myTextViews.count, id: \.self) { _ in
					MyTextView()
				}
			}
			
			Button {
				myTextViews.append(MyTextView())
			} label: {
				Text("Add me!")
			}.padding(.vertical, 10.0)
		}
	}
}

struct MyTextView: View {
	@State var text = ""
	var body: some View {
		ZStack {
			TextField("Enter some text", text: $text)
		}.padding(.horizontal, 50.0)
	}
}

According to the screenshot, I have three instances, each of which contains TextField. After I tap the top button (Show me your current...), I want to show the result from each TextField. How can I do that? Thanks.

Answered by Tomato in 711040022

I've figured it out accidentally.

import SwiftUI

struct ContentView: View {
	@ObservedObject var monster: Monster
	
	var body: some View {
		VStack {
			Button {
				print(monster.items)
			} label: {
				Text("Show me your current text strings")
			}.padding(.vertical, 10.0)
			
			ForEach($monster.items) { item in
				MyTextView(id: item.id, text: item.text).environmentObject(monster)
			}.padding(.horizontal, 200.0)
			
			Button {
				monster.items.append(Keyword())
			} label: {
				Text("Add me!")
			}.padding(.vertical, 10.0)
		}
	}
}

class Monster: ObservableObject {
	@Published var items = [Keyword]()
}

struct Keyword: Identifiable {
	var id = UUID()
	var text = String()
}

struct MyTextView: View {
	@Binding var id: UUID
	@Binding var text: String
	@EnvironmentObject var monster: Monster
	
	var body: some View {
		VStack {
			HStack {
				TextField("Enter text", text: $text)
				Button {
					monster.items.removeAll(where: { $0.id == id} )
				} label: {
					Text("Delete")
				}
			}
		}
	}
}

I need to change MyTextView() to MyTextView(text: "Hello")

So, is it solved now ?

I guess it's something close to the following.

struct ContentView: View {
	@State var textMonsters = [MyTextView]()
	
	var body: some View {
		VStack {
			Button {
				print("Monsters \(textMonsters)")
			} label: {
				Text("Show me your current text strings")
			}.padding(.vertical, 10.0)
			
			ForEach(textMonsters, id: \.self) { textMonster in
				textMonster
			}.padding(.horizontal, 100.0)
			
			Button {
				textMonsters.append(MyTextView(id: UUID(), text: "ABC"))
			} label: {
				Text("Add me!")
			}.padding(.vertical, 10.0)
		}
	}
}

struct MyTextView: View, Identifiable, Hashable {
	var id = UUID()
	var text: String

	var body: some View {
		ZStack {
			TextField("Enter some text", text: Binding(get: { text }, set: { _ in
				
			}))
		}
	}
}

@Claude31 No, sir.

Accepted Answer

I've figured it out accidentally.

import SwiftUI

struct ContentView: View {
	@ObservedObject var monster: Monster
	
	var body: some View {
		VStack {
			Button {
				print(monster.items)
			} label: {
				Text("Show me your current text strings")
			}.padding(.vertical, 10.0)
			
			ForEach($monster.items) { item in
				MyTextView(id: item.id, text: item.text).environmentObject(monster)
			}.padding(.horizontal, 200.0)
			
			Button {
				monster.items.append(Keyword())
			} label: {
				Text("Add me!")
			}.padding(.vertical, 10.0)
		}
	}
}

class Monster: ObservableObject {
	@Published var items = [Keyword]()
}

struct Keyword: Identifiable {
	var id = UUID()
	var text = String()
}

struct MyTextView: View {
	@Binding var id: UUID
	@Binding var text: String
	@EnvironmentObject var monster: Monster
	
	var body: some View {
		VStack {
			HStack {
				TextField("Enter text", text: $text)
				Button {
					monster.items.removeAll(where: { $0.id == id} )
				} label: {
					Text("Delete")
				}
			}
		}
	}
}
Showing Multiple Instances of View and Showing Results from Them
 
 
Q