How to pass data from a TextField to a List (SwiftUI)

I have an app I'm working on where you make little digital ID cards with four pieces of data that I need to pass from a TextField to a List:
  1. Your Name (name)

  2. Your ID for Card (id)

  3. QR Code or Barcode? (qrcode Toggle)

  4. Card Name (cname)

This is my CardsView:
Code Block
import SwiftUI
struct Card: Identifiable {
    let id = UUID()
    let title: String
}
struct CardsView: View {
    @State private var editMode = EditMode.inactive
    @State private var cards: [Card] = []
        private static var count = 0
    var body: some View {
        NavigationView {
            List {
                ForEach(cards) { cards in
                    NavigationLink(destination: CardFullView(cname: "Moore Lunch")) {
                        CardRow(cname: "Lunch", name: "John Doe", id: "123456")
                    }
                }
                .onDelete(perform: onDelete)
                .onMove(perform: onMove)
            }
                .navigationTitle("Cards")
                .toolbar {
                    ToolbarItem(placement: .navigationBarLeading) {
                        EditButton()
                    }
                    ToolbarItem(placement: .navigationBarTrailing) {
                        NavigationLink(
                            destination: AddView(),
                            label: {
                                Image(systemName: "plus")
                            })
                    }
            }
            .environment(\.editMode, $editMode)
        }
    }
    private var addButton: some View {
        switch editMode {
        case .inactive:
            return AnyView(Button(action: onAdd) { Image(systemName: "plus") })
        default:
            return AnyView(EmptyView())
        }
    }
    func onAdd() {
        cards.append(Card(title: "Card #\(Self.count)"))
            Self.count += 1
    }
    private func onDelete(offsets: IndexSet) {
        cards.remove(atOffsets: offsets)
    }
    private func onMove(source: IndexSet, destination: Int) {
        cards.move(fromOffsets: source, toOffset: destination)
    }
}
struct CardsView_Previews: PreviewProvider {
    static var previews: some View {
        CardsView()
    }
}

...and my AddView:
Code Block
import SwiftUI
struct CardFullView: View {
    let cname: String
    var body: some View {
        NavigationView {
            CardView(name: "John Doe", id: "123456")
                .navigationTitle(cname)
                .navigationBarTitleDisplayMode(.inline)
        }
    }
}
struct CardFullView_Previews: PreviewProvider {
    static var previews: some View {
        CardFullView(cname: "Lunch")
    }
}

CardRow shows an HStack, consisting of a generated QR Code or Barcode, a VStack with a title showing cname, a headline showing name, and a subheadline showing id.

I want to pass the value of the TextFields into the CardRows.
Answered by OOPer in 654879022

Sorry for the late reply.

Thanks for clarifying and sorry for missing the reply.

Most parts get cleared, but it seems you have touched your code since then.

There's another bug happening and I do not understand what this means or why you do not want it.

I figured out a way to show the cardInfo data in the list, but it's not the way I want it

I think it's time for you to start your new thread to solve such another thing.
Please do not forget to include all the latest code when starting a new thread.
One last thing;
What would I do so (first) it enables the button when you fill in everything and when you (second) press the button, it adds to the list with the completed CardsInfo data?

One last thing;

You need to clarify a few things to make your last thing last.
  • Where is the button?

  • Where is the list?

Sorry for the late reply.

  • Where is the button?

  • Where is the list?

In AddView, the "Create" button is located at the bottom. Here it is with all its modifiers:
Code Block
Button(action: {}) {
Text("Create")
.bold()
}.disabled(self.cardInfo.name.isEmpty self.cardInfo.id.isEmpty self.cardInfo.cname.isEmpty)
.foregroundColor(.white)
.padding()
.padding(.horizontal, 100)
.background(Color.accentColor)
.cornerRadius(10)

The disabled modifier is disabling it entirely, even if I add a value to the TextFields. I need some way to change that. The || dividers aren't showing because it screws up the code block.

On my second thing, there is no action specified. That's what I'd like to replace with something to have the now completed (and, correct me if I'm wrong) cardInfo to pass through CardRow and end up in the List that is located in CardsView:
Code Block
List {
ForEach(cards) { cards in
NavigationLink(destination: CardFullView(cname: cardsInfo.newCard.cname)) {
CardRow(cname: cardsInfo.newCard.cname, name: cardsInfo.newCard.name, id: cardsInfo.newCard.id)
}
}
.onDelete(perform: onDelete)
.onMove(perform: onMove)
}

The disabled modifier is disabling it entirely, even if I add a value to the TextFields. I need some way to change that. 

There's another bug happening with this, as well. Though when initially putting in the info, it doesn't enable the Create button, nor does it update the card preview (CardView) and navigationTitle. But when dismissing it and tapping the add button again, it shows that information and enables the Create button.

On another note, I figured out a way to show the cardInfo data in the list, but it's not the way I want it. I want some way to make it so the Create button executes the onAdd function...and it seems finished from what I've tested.

I've changed it so AddView shows as a Sheet, so it's no longer a NavigationLink. All I need to do is call onAdd, but that function is embedded in CardsView, where the List is located. I want to call that function from AddView, but it's only available in CardsView.
Accepted Answer

Sorry for the late reply.

Thanks for clarifying and sorry for missing the reply.

Most parts get cleared, but it seems you have touched your code since then.

There's another bug happening and I do not understand what this means or why you do not want it.

I figured out a way to show the cardInfo data in the list, but it's not the way I want it

I think it's time for you to start your new thread to solve such another thing.
Please do not forget to include all the latest code when starting a new thread.

Most parts get cleared, but it seems you have touched your code since then.

You're correct, as I was revamping some views and disregarded important parts. That's entirely my fault.

I think it's time for you to start your new thread to solve such another thing.

I agree, I feel I've strayed away from what I've been trying to solve...I will start another thread to expand.

Edit:
Here is the new thread:
https://developer.apple.com/forums/thread/670536
How to pass data from a TextField to a List (SwiftUI)
 
 
Q